Looking for help with regular expressions- not Python Specific

Roy Smith roy at panix.com
Thu Jan 8 09:24:58 EST 2004


cappy2112 at yahoo.com (Tony C) wrote:
> But I would like to find a mailing list or newsgroup where I can ask
> questions about regexps (when things don't work), not specifically
> dealing with Python. When I have Python-regexp questions, I'll post
> them here of course.

I don't know of any regex-specific newsgroups or mailing lists, but most 
old unix hands are pretty good at them, so asking in comp.unix.questions 
might be worth trying.  Perl makes extensive use of regex, so I'll bet 
you'll find some regex gurus on comp.lang.perl too.

Try doing a search on groups.google.com for regex and see what 
newsgroups pop up the most with recent dates, then ask your questions 
there.  Keep in mind that there are many different regex libraries out 
there, and they don't all accept exactly the same syntax.  Python and 
Perl use the same library (I believe).

But, on the other hand, we're a pretty friendly group on 
comp.lang.python, and I know there's a bunch of regex wizards who hang 
out here as well, so why not just ask here.

One Python-specific tip is to get into the habit of ALWAYS using the r'' 
style raw strings for regex.  It makes life so much simplier.

Joe Francia <usenet at -OBFUSCATED-joefrancia.com> wrote:
> First, be sure you actually need regexes.  See if you can achieve the 
> same results using string methods.  String methods are faster to run, 
> easier to write, and much easier to debug.

Regex's are an extremely powerful tool, and anybody who's serious about 
programming (especially if you do text processing) should have a solid 
mastery of them.  That being said, Joe is certainly right about them not 
always being the best tool for the job.  Chainsaws and scalpels are both 
useful cutting tools, but hardly interchangable in all applications.  
Regex's can lead to extremely fast and compact code, but then can also 
lead to stuff which is impossible for anybody to understand 6 months 
later.

Also, Joe's comment that string methods are faster to run is a 
half-truth.  Regex's get compiled.  If you use the all-in-one step regex 
methods, you compile the expression every time you use it.  Compiling is 
expensive.  But, if you pre-compile the expression, using it is very 
fast.  Doing the compile step once, and re-using the result can be very 
fast.  A single regex that looks for a complex pattern may well be 
faster than a series of string methods tied together with conditional 
logic.  Your mileage may vary, so if speed is important to you, try 
different ways and profile it.

In any case, for the vast majority of stuff, speed is just not an issue, 
and you should pick your weapon based more on clarity and simplicity of 
the resulting code than on raw speed.



More information about the Python-list mailing list