OT: Perl Compatible Regular Expressions

Chris Rees utisoft at gmail.com
Sun Dec 18 14:06:33 GMT 2011

On 18 December 2011 13:47, Paul Welsh <paul at welshfamily.com> wrote:
> On 18 December 2011 12:00,  Chris Rees <utisoft at gmail.com> wrote:
>> mail[0-9]*.domain.com should do?
>> Are you trying to match mail.domain.com too? If you don't want to
>> match that, then use + instead of *
> Hi Chris
> Thanks very much for responding.  Can you just explain what the:
> [0-9]*
> actually means?
> To give some more real world examples, I'd need to match:
> mail-01.domain.com, mail-02.domain.com ... mail-15.domain.com
> This would take one line in the whitelist file using regular
> expressions versus 15 separate IP addresses or FQDNs.
> Another real world example:
> smtp-out-81-59.amazon.com, smtp-out-81-60.amazon.com, smtp-out-81-61.amazon.com

OK, although this isn't a list for regexes I'll give a quick
explanation, since it could help others too :)

A regex (basically) uses characters and modifiers. * is a modifier, it
doesn't match anything on its own. [0-9] is a *character class*, which
matches any digit, and when put with the * modifier it matches any
number of digits (including none).

If you were to use [0-9]+, then the + modifier will make [0-9] match
*one or more* digits; the difference between * and + is that * matches
no digit whereas + doesn't.

Example regexes:

smtp-out-81-59.amazon.com, smtp-out-81-60.amazon.com, smtp-out-81-61.amazon.com


mail-01.domain.com, mail-02.domain.com ... mail-15.domain.com


(or to be more fussy and only match 1-15)

(using the beginning delimiter ^ and end delimiter $ makes the regex
much faster too since if the first character fails to match it skips
that regex and also stops matching mail13.domain.com.evil.scammer.com)

To be honest, regular expressions have to be learned in their entirety
to be properly useful, and I'd recommend Mastering Regular Expressions
[1] as a great way to learn them and make them less scary.


[1] http://shop.oreilly.com/product/9781565922570.do

More information about the MailScanner mailing list