我真的很难为此贴上标签,这可能就是我无法通过搜索找到所需内容的原因。
我希望匹配以下内容:
我正在使用的平台不允许指定不区分大小写的搜索。我尝试了以下正则表达式:
.*[aA]uto(?:matic)[ ]*[rR]eply.*
认为(?:matic)
会使我的表达与Auto
或Automatic
匹配。但是,它只匹配Automatic
。
这是使用Perl作为正则表达式引擎(我认为是PCRE
,但我不确定)。
答案 0 :(得分:23)
(?:...)
是正则表达式模式,(...)
是算术:它只是覆盖优先级。
ab|cd # Matches ab or cd
a(?:b|c)d # Matches abd or acd
?
量词是匹配可选的原因。
a? # Matches a or an empty string
abc?d # Matches abcd or abd
a(?:bc)?d # Matches abcd or ad
你想要
(?:matic)?
如果没有不必要的前导和尾随.*
,我们会得到以下结果:
/[aA]uto(?:matic)?[ ]*[rR]eply/
正如@ adamdc78指出的那样,匹配AutoReply
。使用以下内容可以避免这种情况:
/[aA]uto(?:matic[ ]*|[ ]+)[rR]eply/
答案 1 :(得分:4)
这应该有效:
/.*[aA]uto(?:matic)? *[rR]eply/
您在?
(?:matic)
答案 2 :(得分:3)
[Aa]uto(?:matic ?| )[Rr]eply
这假设您不希望AutoReply
成为有效命中。
你只是错过了正则表达式中的optional ("?")。如果您希望在回复后匹配整行,那么最后包括.*
就可以了,但您的问题没有说明您要查找的内容。
答案 3 :(得分:2)
您可以将此正则表达式用于行开始/结束锚点:
^[aA]uto(?:matic)? *[rR]eply$
<强>解释强>
^ assert position at start of the string
[aA] match a single character present in the list below
aA a single character in the list aA literally (case sensitive)
uto matches the characters uto literally (case sensitive)
(?:matic)? Non-capturing group
Quantifier: Between zero and one time, as many times as possible, giving back as needed
[greedy]
matic matches the characters matic literally (case sensitive)
* matches the character literally
Quantifier: Between zero and unlimited times, as many times as possible, giving back
as needed [greedy]
[rR] match a single character present in the list below
rR a single character in the list rR literally (case sensitive)
eply matches the characters eply literally (case sensitive)
$ assert position at end of the string
答案 4 :(得分:1)
略有不同。结果相同。
m/([aA]uto(matic)? ?[rR]eply)/
经过测试:
Some other stuff....
Auto Reply
Automatic Reply
AutomaticReply
Now some similar stuff that shouldn't match (auto).