OT: Perl Compatible Regular Expressions
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:
> 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.
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
 as a great way to learn them and make them less scary.
More information about the MailScanner