小编bor*_*ryn的帖子

正则表达式捕获的负面后向问题

我尝试匹配电子邮件地址,但前提是它们不以“ mailto:”开头。我尝试这个正则表达式:

"/(?<!mailto:)[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/"

针对此字符串: '<a href="mailto:someemail@domain.com">EMAIL</a> ... otheremail@domain.com '

我本来只希望抓住'otheremail@domain.com',但我也收到'omeemail@domain.com'-见失踪's'。我不知道这是怎么了。后置断言后,我不能使用正常的正则表达式吗?

我在PHP中的整个示例如下:

$testString = '<a href="mailto:someemail@domain.com">EMAIL</a>  ...   otheremail@domain.com ';
$pattern = "/(?<!mailto:)[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/";
preg_match_all($pattern, $testString, $matches);
echo('<pre>');print_r($matches);echo('</pre>');
Run Code Online (Sandbox Code Playgroud)

谢谢!

php regex negative-lookbehind

4
推荐指数
1
解决办法
3216
查看次数

标签 统计

negative-lookbehind ×1

php ×1

regex ×1