Regular Expression that matches a word, or nothing

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

Tags:

Regex

Perl

Pcre