In article <[email protected]>,
Gregory Ewing <[email protected]> wrote:
> John Bokma wrote:
>
> > A Perl programmer will call this line noise:
> >
> > double_word_re = re.compile(r"\b(?P<word>\w+)\s+(?P=word)(?!\w)",
> > re.IGNORECASE)
One of the truly awesome things about the Python re library is that it
lets you write complex regexes like this:
pattern = r"""\b # beginning of line
(?P<word>\w+) # a word
\s+ # some whitespace
(?P=word)(?!\w) # the same word again
"""
double_word_re = re.compile(pattern, re.I | re.X)
Sometimes regex really is the best tool. It's often the most compact,
or fastest, or clearest way to express something complicated.
Fortunately, re.X mode gives you a way to write truly monster regexes
and still having them not be total line noise.
It's a shame that the Python community has evolved to be so anti-regex
that most people never consider using them. While Perl's attitude to
regex may be "when the only tool you have is a hammer, everything looks
like a nail", Python's seems to be, "I've got a great collection of all
kinds of neat tools, so I'm going to pretend the hammer that's in there
doesn't exist because I once smashed my thumb with it and it hurt a lot".
--
http://mail.python.org/mailman/listinfo/python-list