Вопрос

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.

  • 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).

Это было полезно?

Решение

(?:...) 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:

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

or

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

Другие советы

This should work:

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

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

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

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

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.

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

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).
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top