What is Python?

Andrew Dalke dalke at acm.org
Wed Sep 20 15:47:55 EDT 2000


William "Billy" Tanksley:
>We saw an example of this bloat just a while ago, when someone implemented
>your search without regexps.  Someone preferring that mess to regexps
>would be having problems.

Since I think you're referring to the code I posted, let me chime in
that I agree.  The regexp solution is much simpler, easier to understand,
less error-prone, etc and is the Right Thing for this problem.

Addressing Tim Hammerquist's post, and bear in mind that I misremember the
details of the following story.

A few weeks ago, someone posted a question about parsing a file with lines
of the form "CityName phone_number" where the city name can contain spaces,
(like "Santa Fe") but the phone number was one word.  He wanted to parse
the line with a regexp like (untested)

 ^(.*) ([^ ]*)$

which works, but depends on backtracking.  The more appropriate solution,
IMHO, was:

words = string.split(line[:-1])
city = string.join(words[:-1])
phone = words[-1]

This is longer code, but the intent is more obvious and doesn't require
that you understand backtracking.  This becomes more useful if the format
is "CityName phone_number zip_code" where the last *two* fields don't
contain
spaces.  The regexp extension of the above requires two levels of
backtracking, and starts to slow down while the explicit split/join solution
continues to work as expected.

BTW, this is not the same solution as the regexp one.  For example, if the
city was "Santa    Fe" it would be turned into "Santa Fe".  That can be
considered a feature :)

                    Andrew
                    dalke at acm.org


                    Andrew
                    dalke at acm.org







More information about the Python-list mailing list