Regex,获取两个字符之间的字符串值

时间:2022-11-27 18:41:26

I'd like to return string between two characters, @ and dot (.).

我想返回两个字符之间的字符串@和dot(.)。

I tried to use regex but cannot find it working.

我尝试使用regex,但发现它不能正常工作。

(@(.*?).)

Anybody?

有人知道吗?

4 个解决方案

#1


31  

Your regular expression almost works, you just forgot to escape the period. Also, in PHP you need delimiters:

你的正则表达式几乎是有效的,你只是忘记了转圈。另外,在PHP中需要分隔符:

'/@(.*?)\./s'

The s is the DOTALL modifier.

s是DOTALL修饰符。

Here's a complete example of how you could use it in PHP:

下面是如何在PHP中使用它的完整示例:

$s = 'foo@bar.baz';
$matches = array();
$t = preg_match('/@(.*?)\./s', $s, $matches);
print_r($matches[1]);

Output:

输出:

bar

#2


11  

Try this regular expression:

试试这个正则表达式:

@([^.]*)\.

The expression [^.]* will match any number of any character other than the dot. And the plain dot needs to be escaped as it’s a special character.

表达式[^。将匹配除点之外的任何字符的任何数量。这个普通点需要转义,因为它是一个特殊的字符。

#3


2  

If you're learning regex, you may want to analyse those too:

如果你正在学习regex,你也可以分析一下:

@\K[^.]++(?=\.)

(?<=@)[^.]++(?=\.)

Both these regular expressions use possessive quantifiers (++). Use them whenever you can, to prevent needless backtracking. Also, by using lookaround constructions (or \K), we can match the part between the @ and the . in $matches[0].

这两个正则表达式都使用所有格量词(++)。尽可能使用它们,避免不必要的回溯。此外,通过使用lookaround结构(或\K),我们可以匹配@和the之间的部分。在$ matches[0]。

#4


2  

this is the best and fast to use

这是最好和快速使用

function get_string_between ($str,$from,$to) {

    $string = substr($str, strpos($str, $from) + strlen($from));

    if (strstr ($string,$to,TRUE) != FALSE) {

        $string = strstr ($string,$to,TRUE);

    }

    return $string;

}

#1


31  

Your regular expression almost works, you just forgot to escape the period. Also, in PHP you need delimiters:

你的正则表达式几乎是有效的,你只是忘记了转圈。另外,在PHP中需要分隔符:

'/@(.*?)\./s'

The s is the DOTALL modifier.

s是DOTALL修饰符。

Here's a complete example of how you could use it in PHP:

下面是如何在PHP中使用它的完整示例:

$s = 'foo@bar.baz';
$matches = array();
$t = preg_match('/@(.*?)\./s', $s, $matches);
print_r($matches[1]);

Output:

输出:

bar

#2


11  

Try this regular expression:

试试这个正则表达式:

@([^.]*)\.

The expression [^.]* will match any number of any character other than the dot. And the plain dot needs to be escaped as it’s a special character.

表达式[^。将匹配除点之外的任何字符的任何数量。这个普通点需要转义,因为它是一个特殊的字符。

#3


2  

If you're learning regex, you may want to analyse those too:

如果你正在学习regex,你也可以分析一下:

@\K[^.]++(?=\.)

(?<=@)[^.]++(?=\.)

Both these regular expressions use possessive quantifiers (++). Use them whenever you can, to prevent needless backtracking. Also, by using lookaround constructions (or \K), we can match the part between the @ and the . in $matches[0].

这两个正则表达式都使用所有格量词(++)。尽可能使用它们,避免不必要的回溯。此外,通过使用lookaround结构(或\K),我们可以匹配@和the之间的部分。在$ matches[0]。

#4


2  

this is the best and fast to use

这是最好和快速使用

function get_string_between ($str,$from,$to) {

    $string = substr($str, strpos($str, $from) + strlen($from));

    if (strstr ($string,$to,TRUE) != FALSE) {

        $string = strstr ($string,$to,TRUE);

    }

    return $string;

}