0

我需要如果字符串包含符号.并且@打印是,队列符号无关紧要,重要的是在字符串中这个符号至少会出现一次,我需要使用正则表达式进行此操作。

我写的:

if (preg_match("#(\@.*\.)|(\..*\@)#",$str)) {
    echo "yes";
}

但我怀疑是否可以为此编写更简单的模式

4

3 回答 3

2

使用这个正则表达式(?=.*\.)(?=.*@).+

(?=.*\.) dot exists
(?=.*@) @ exists
.+ any string
于 2013-01-07T17:51:35.503 回答
2

You can use lookahead to separate the two conditions:

^(?=.*\.)(?=.*@)

The start-of-string anchor is not needed, but it helps performance.

于 2013-01-07T17:52:40.327 回答
0

Perhaps its a bit easier without a regex.

if(strpos($str, ".") !== false || strpos($str, "@") !== false) {
    echo "yes";
}

Then you don't need the regex is perhaps a bit faster. Then you only search if a character is in a string.

于 2013-01-07T17:54:09.093 回答