OT: REGEXP
Matt Kettler
mkettler at evi-inc.com
Tue Dec 12 22:45:22 GMT 2006
Peter Russell wrote:
> I have postfix recipient maps and some one on here once helped me format
> the content using regexp so i could create one entry that covered
> multiple formats. I need to change that but am almost clueless as to how.
>
> Could some one offer a suggestion on how to write the following as a
> regexp.
>
> Current Required
> user at domain.com OK user at domain.com OK
> user at domain.com.au OK
>
> Is the REGEXP something like
> /user at domain/.[com|/.com/.au] OK
Disclaimer: I know regexes VERY well, but postfix not at all.
That regex is pretty hosed.
1) use \ not / in front of .'s that you want to be literals. / begins and ENDS
the regex.
Fixing error 1 we get:
/user at domain\.[com|\.com\.au]/
2) [] is used for character ranges, so [com|/.com/.au] will match 1 character,
as long as it's one of the characters contained between the braces. You really
want () for this kind of thing.
Fixing error 2 we get:
/user at domain\.(com|\.com\.au)/
3) you have a logic bomb in the () section here. In order to match "com.au"
you'd wind up matching domain..com.au. Two .'s required because there's already
one out before the ()'s.
Fixing error 3:
/user at domain\.(com|com\.au)/
4) Inefficient use of (|). Both clauses start with "com". Ordinarily you'd
eliminate that and move it outside the (), but this would make the first clause
empty.. so, we'll ditch the (|) entirely and use ()? instead.
/user at domain\.com(\.au)?/
5) Inefficient use of a trailing ? expression. Generally speaking regexes match
substrings, so any ? or * expression at the beginning or end of a regex is
irrelevant. Provided there's not some weirdness in postfix that forces regexes
to match full-text, you can simplify further to:
/user at domain\.com/
Which would match "user at domain.com" "user at domain.com.au" or
even"anythingintheworld-user at domain.com-anythingelseintheworld".
6) If the substring matching pointed out in 5 is over-broad for you, you can fix
4 by ending anchors for beginning and end of input:
/^user at domain\.com(\.au)?$/
That will only match exactly "user at domain.com" or "user at domain.com.au"
More information about the MailScanner
mailing list