我需要如果字符串包含符号.
并且@
打印是,队列符号无关紧要,重要的是在字符串中这个符号至少会出现一次,我需要使用正则表达式进行此操作。
我写的:
if (preg_match("#(\@.*\.)|(\..*\@)#",$str)) {
echo "yes";
}
但我怀疑是否可以为此编写更简单的模式
使用这个正则表达式(?=.*\.)(?=.*@).+
(?=.*\.) dot exists
(?=.*@) @ exists
.+ any string
You can use lookahead to separate the two conditions:
^(?=.*\.)(?=.*@)
The start-of-string anchor is not needed, but it helps performance.
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.