The Regex Story

Tim Chase python.list at tim.thechases.com
Thu Apr 8 10:23:39 EDT 2010


Lie Ryan wrote:
> Why am I seeing a lot of this pattern lately:
> 
> OP: Got problem with string
> +- A: Suggested a regex-based solution
>    +- B: Quoted "Some people ... regex ... two problems."
> 
> or
> 
> OP: Writes some regex, found problem
> +- A: Quoted "Some people ... regex ... two problems."
>    +- B: Supplied regex-based solution, clean one
>       +- A: Suggested PyParsing (or similar)

There's a spectrum of parsing solutions:

- string.split() or string[slice] notations handle simple cases 
and are built-in

- regexps handle more complex parsing tasks and are also built in

- pyparsing handles far more complex parsing tasks (nesting, etc) 
but isn't built-in


The above dialog tends to appear when the task isn't in the 
sweet-spot of regexps.  Either it's sufficiently simple that 
simple split/slice notation will do, or (at the other end of the 
spectrum) the effort to get it working with a regexp is hairy and 
convoluted, worthy of a more readable solution implemented with 
pyparsing.  The problem comes from people thinking that regexps 
are the right solution to *every* problem...often demonstrated by 
the OP writing "how do I write a regexp to solve this 
<non-regexp-optimal> problem" assuming regexps are the right tool 
for everything.

There are some problem-classes for which regexps are the *right* 
solution, and I don't see as much of your example dialog in those 
cases.

-tkc





More information about the Python-list mailing list