Regular Expression

Roy Smith roy at panix.com
Wed Dec 15 08:59:39 EST 2004


Michael McGarry <replytogroup at nospam.org> wrote:

> I am horrible with Regular Expressions, can anyone recommend a book on it?

I just did a search on the Barnes & Noble site for "regular expression" 
and came up with a bunch of books.  The following looks reasonable:

http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?userid=Ft1ixM
aAY9&isbn=0596002890&itm=1

but I find the blub kind of funny.  It says:

>  Despite their wide availability, flexibility, and unparalleled 
>  power, regular expressions are frequently underutilized. Regular 
>  expressions allow you to code complex and subtle text processing 
>  that you never imagined could be automated. Regular expressions can 
>  save you time and aggravation. They can be used to craft elegant 
>  solutions to a wide range of problems.

On the other hand, they are frequently overused.  Back in the days of 
steam-powered computers, utilities like ed and grep ruled the landscape.  
The only text parsing tool we had was regular expressions, so that's 
what we used for everything, and we got good at them out of necessity.  
Every once in a while we came upon a problem regexs couldn't solve, so 
our toolbuilders built us ever more powerful regex libraries (Henry 
Spencer's version probably being the best) allowing us to write ever 
more complex regular expressions.

These days, regex is still a powerful tool, and undeniably the best tool 
in certain situations.  But there are simplier and easier tools for many 
every-day jobs.  I think this example is one of those.
 
> Also I am trying to parse the following string to extract the number 
> after load average.
> 
> ".... load average: 0.04, 0.02, 0.01"

I would just use the split method of strings:

>>> s = ".... load average: 0.04, 0.02, 0.01"
>>> words = s.split()
>>> load = words[3].strip (',')
>>> print load
'0.04'

You could do this with regex if you wanted to.  One way would be to use 
regex in a simple way to deal with the fact that the "words" are 
delimited by a combination of spaces and commas:

>>> import re
>>> words = re.split ('[, ]*', s)
>>> load = words[3]
>>> print load
'0.04'

which may or may not be any easier to understand.  Another way would be 
to use a more complex regex to match exactly the piece you want in one 
step:

>>> load = re.findall (r': ([^,]*)', s)
>>> print load

This does the whole job in a single line, but depending on how familier 
with regex's you are, it may or may not be easier to understand.  Note, 
that there is a fundamental difference between what this is doing and 
what was being done above.  In the last example, the location of the 
load average string is determined by looking after the ':' for something 
that fit a pattern.  In the earlier two examples, it was found by 
counting words from the beginning of the string.  If the stuff that came 
before the ':' might have a variable number of words in it, the full 
regex version might be more correct.



More information about the Python-list mailing list