Regular Expressions

Gabriel Genellina gagsl-py at yahoo.com.ar
Sun Feb 11 13:13:22 EST 2007


En Sun, 11 Feb 2007 13:35:26 -0300, deviantbunnylord at gmail.com  
<deviantbunnylord at gmail.com> escribió:

>> (Steven?)
>> 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.
>
> 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?

For very simple things, it's easier/faster to use string methods like find  
or split. By example, splitting "2007-02-11" into y,m,d parts:
y,m,d = date.split("-")
is a lot faster than matching "(\d+)-(\d+)-(\d+)"
On the other hand, complex tasks like parsing an HTML/XML document,  
*can't* be done with a regexp alone; but people insist anyway, and then  
complain when it doesn't work as expected, and ask how to "fix" the  
regexp...
Good usage of regexps maybe goes in the middle.

-- 
Gabriel Genellina




More information about the Python-list mailing list