regex help

Intchanter / Daniel Fackrell unlearned at gmail.com
Wed Dec 16 12:38:42 EST 2009


On Dec 16, 10:22 am, r0g <aioe.... at technicalbloke.com> wrote:
> Gabriel Rossetti wrote:
> > Hello everyone,
>
> > I'm going nuts with some regex, could someone please show me what I'm
> > doing wrong?
>
> > I have an XMPP msg :
>
> <snip>
>
> > Does someone know what is wrong with my expression? Thank you, Gabriel
>
> Gabriel, trying to debug a long regex in situ can be a nightmare however
> the following technique always works for me...
>
> Use the interactive interpreter and see if half the regex works, if it
> does your problem is in the second half, if not it's in the first so try
> the first half of that and so on an so forth. You'll find the point at
> which it goes wrong in a snip.
>
> Non-trivial regexes are always best built up and tested a bit at a time,
> the interactive interpreter is great for this.
>
> Roger.

I'll just add that the "now you have two problems" quip applies here,
especially when there are very good XML parsing libraries for Python
that will keep you from having to reinvent the wheel for every little
change.

See sections 20.5 through 20.13 of the Python Documentation for
several built-in options, and I'm sure there are many community
projects that may fit the bill if none of those happen to.

Personally, I consider regular expressions of any substantial length
and complexity to be bad practice as it inhibits readability and
maintainability.  They are also decidedly non-Zen on at least
"Readability counts" and "Sparse is better than dense".

Intchanter
Daniel Fackrell

P.S. I'm not sure how any of these libraries are implemented yet, but
I'd hope they're using a finite state machine tailored to the parsing
task rather than using regexes, but even if they do the latter, having
that abstracted out in a mature library with a clean interface is
still a huge win.



More information about the Python-list mailing list