gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:28:03 24 4
gpt4 key购买 nike

我有这个正则表达式:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com