regex - Perl 字符串模式匹配的负正则表达式

标签 regex perl

我有这个正则表达式:

if($string =~ m/^(Clinton|[^Bush]|Reagan)/i)
  {print "$string\n"};

我想与克林顿和里根匹配,但不想与布什匹配。

它不起作用。

最佳答案

你的正则表达式不起作用,因为 [] 定义了一个字符类,但你想要的是一个前瞻:

(?=) - Positive look ahead assertion foo(?=bar) matches foo when followed by bar
(?!) - Negative look ahead assertion foo(?!bar) matches foo when not followed by bar
(?<=) - Positive look behind assertion (?<=foo)bar matches bar when preceded by foo
(?<!) - Negative look behind assertion (?<!foo)bar matches bar when NOT preceded by foo
(?>) - Once-only subpatterns (?>\d+)bar Performance enhancing when bar not present
(?(x)) - Conditional subpatterns
(?(3)foo|fu)bar - Matches foo if 3rd subpattern has matched, fu if not
(?#) - Comment (?# Pattern does x y or z)

所以尝试一下:(?!布什)

关于regex - Perl 字符串模式匹配的负正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6361312/

相关文章:

JavaScript 正则表达式结果

ruby - 如何更改此正则表达式以从未指定 v 参数的 Youtube URL 获取 Youtube 视频 ID?

java - 使用 Java 正则表达式解析带分隔符的字符串

multithreading - Perl - 多线程/ fork /同步问题 :

jquery - 如何使用 jQuery 模糊事件向 Perl 脚本发出 AJAX 请求?

html - 我需要编码或解码吗?

html - 使用 Perl 从 MySQL 填充下拉列表

android - Android 中用于 EditText 的 InputFilter 的正则表达式帮助

windows - 批处理文件中的 CSV 格式

perl - 当 token 之间没有值时,Perl 拆分函数返回什么?