正则表达式匹配单词,或什么都不匹配

时间:2021-08-07 20:25:24

I'm really struggling to put a label on this, which is probably why I was unable to find what I need through a search.

我真的很难在这上面贴上标签,这可能就是为什么我无法通过搜索找到我需要的东西。

I'm looking to match the following:

我想要匹配以下内容:

  • Auto Reply
  • 自动回复
  • Automatic Reply
  • 自动回复
  • AutomaticReply
  • 自动回复

The platform that I'm using doesn't allow for the specification of case-insensitive searches. I tried the following regular expression:

我正在使用的平台不允许指定不区分大小写的搜索。我尝试了以下正则表达式:

.*[aA]uto(?:matic)[ ]*[rR]eply.*

Thinking that (?:matic) would cause my expression to match Auto or Automatic. However, it is only matching Automatic.

认为(?:matic)会使我的表达式与自动或自动匹配。但是,它只匹配自动。

  • What am I doing wrong?
  • 我究竟做错了什么?
  • What is the proper terminology here?
  • 这里的术语是什么?

This is using Perl for the regular expression engine (I think that's PCRE but I'm not sure).

这是使用Perl作为正则表达式引擎(我认为这是PCRE,但我不确定)。

5 个解决方案

#1


22  

(?:...) is to regex patterns as (...) is to arithmetic: It simply overrides precedence.

(?:...)是正则表达式模式,因为(...)是算术:它只是覆盖优先级。

 ab|cd        # Matches ab or cd
 a(?:b|c)d    # Matches abd or acd

A ? quantifier is what makes matching optional.

一个 ?量词是使匹配可选的原因。

 a?           # Matches a or an empty string
 abc?d        # Matches abcd or abd
 a(?:bc)?d    # Matches abcd or ad

You want

你要

(?:matic)?

Without the needless leading and trailing .*, we get the following:

没有不必要的前导和尾随。*,我们得到以下内容:

/[aA]uto(?:matic)?[ ]*[rR]eply/

As @adamdc78 points out, that matches AutoReply. This can be avoided as using the following:

正如@ adamdc78所指出的那样,它与AutoReply相匹配。使用以下内容可以避免这种情况:

/[aA]uto(?:matic[ ]*|[ ]+)[rR]eply/

#2


4  

This should work:

这应该工作:

/.*[aA]uto(?:matic)? *[rR]eply/

you were simply missing the ? after (?:matic)

你只是错过了?之后(?:matic)

#3


3  

[Aa]uto(?:matic ?| )[Rr]eply

This assumes that you do not want AutoReply to be a valid hit.

这假设您不希望AutoReply成为有效命中。

You're just missing the optional ("?") in the regex. If you're looking to match the entire line after the reply, then including the .* at the end is fine, but your question didn't specify what you were looking for.

你只是缺少正则表达式中的可选(“?”)。如果您希望在回复后匹配整行,那么在最后包括。*就可以了,但您的问题没有说明您要查找的内容。

#4


2  

You can use this regex with line start/end anchors:

您可以将此正则表达式与行开始/结束锚点一起使用:

^[aA]uto(?:matic)? *[rR]eply$

Explanation:

说明:

^ 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

#5


1  

Slightly different. Same result.

稍微不一样。结果相同。

m/([aA]uto(matic)? ?[rR]eply)/

Tested Against:

经测试:

Some other stuff....
    Auto Reply
    Automatic Reply
    AutomaticReply

Now some similar stuff that shouldn't match (auto).

#1


22  

(?:...) is to regex patterns as (...) is to arithmetic: It simply overrides precedence.

(?:...)是正则表达式模式,因为(...)是算术:它只是覆盖优先级。

 ab|cd        # Matches ab or cd
 a(?:b|c)d    # Matches abd or acd

A ? quantifier is what makes matching optional.

一个 ?量词是使匹配可选的原因。

 a?           # Matches a or an empty string
 abc?d        # Matches abcd or abd
 a(?:bc)?d    # Matches abcd or ad

You want

你要

(?:matic)?

Without the needless leading and trailing .*, we get the following:

没有不必要的前导和尾随。*,我们得到以下内容:

/[aA]uto(?:matic)?[ ]*[rR]eply/

As @adamdc78 points out, that matches AutoReply. This can be avoided as using the following:

正如@ adamdc78所指出的那样,它与AutoReply相匹配。使用以下内容可以避免这种情况:

/[aA]uto(?:matic[ ]*|[ ]+)[rR]eply/

#2


4  

This should work:

这应该工作:

/.*[aA]uto(?:matic)? *[rR]eply/

you were simply missing the ? after (?:matic)

你只是错过了?之后(?:matic)

#3


3  

[Aa]uto(?:matic ?| )[Rr]eply

This assumes that you do not want AutoReply to be a valid hit.

这假设您不希望AutoReply成为有效命中。

You're just missing the optional ("?") in the regex. If you're looking to match the entire line after the reply, then including the .* at the end is fine, but your question didn't specify what you were looking for.

你只是缺少正则表达式中的可选(“?”)。如果您希望在回复后匹配整行,那么在最后包括。*就可以了,但您的问题没有说明您要查找的内容。

#4


2  

You can use this regex with line start/end anchors:

您可以将此正则表达式与行开始/结束锚点一起使用:

^[aA]uto(?:matic)? *[rR]eply$

Explanation:

说明:

^ 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

#5


1  

Slightly different. Same result.

稍微不一样。结果相同。

m/([aA]uto(matic)? ?[rR]eply)/

Tested Against:

经测试:

Some other stuff....
    Auto Reply
    Automatic Reply
    AutomaticReply

Now some similar stuff that shouldn't match (auto).