我有这个正则表达式:
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/