Regular Expressions

Steve Holden steve at holdenweb.com
Sun Feb 11 16:49:01 EST 2007


deviantbunnylord at gmail.com wrote:
>> That's a little harsh -- regexes have their place, together with pointer
>> arithmetic, bit manipulations, reverse polish notation and goto. The
>> problem is when people use them inappropriately e.g. using a regex when a
>> simple string.find will do.
>>
>>> A quote attributed variously to
>>> Tim Peters and Jamie Zawinski says "Some people, when confronted with a
>>> problem, think 'I know, I'll use regular expressions.' Now they have two
>>> problems."
>> I believe that is correctly attributed to Jamie Zawinski.
>>
>> --
>> Steven
> 
> So as a newbie, I have to ask. I've played with the re module now for
> a while, I think regular expressions are super fun and useful. As far
> as them being a problem I found they can be tricky and sometimes the
> regex's I've devised do unexpected things...(which I can think of two
> instances where that unexpected thing was something that I had hoped
> to get into further down the line, yay for me!). So I guess I don't
> really understand why they are a "bad idea" to use. I don't know of
> any other way yet to parse specific data out of a text, html, or xml
> file without resorting to regular expressions.
> What other ways are there?
> 
Re's aren't inherently bad. Just avoid using them as a hammer to the 
extent that all your problems look like nails.

They wouldn't exist if there weren't problems it was appropriate to use 
them on. Just try to use simpler techniques first.

For example, don't use re's to find out if a string starts with a 
specific substring when you could instead use the .startswith() string 
method.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Blog of Note:          http://holdenweb.blogspot.com
See you at PyCon?         http://us.pycon.org/TX2007




More information about the Python-list mailing list