builtin regular expressions?

Paddy paddy3118 at netscape.net
Sun Oct 1 08:40:16 EDT 2006


Antoine De Groote wrote:
> Hello,
>
> Can anybody tell me the reason(s) why regular expressions are not built
> into Python like it is the case with Ruby and I believe Perl? Like for
> example in the following Ruby code
>
> line = 'some string'
>
> case line
>    when /title=(.*)/
>      puts "Title is #$1"
>    when /track=(.*)/
>      puts "Track is #$1"
>    when /artist=(.*)/
>      puts "Artist is #$1"
> end
>
> I'm sure there are good reasons, but I just don't see them.
>
I'd say that Ruby took a lot of its early design clues from Perl. If
you want to attract Perl programmers then they expect such things.
Python has an emphasis on clarity/readability and on maintainability.
It has other string methods that are much easier to read and maintain
than regular expressions.
There is of course a class of problem well suited to regular
expressions but it is easy for people coming to Python from Perl or AWK
to rely too heavily on RE's.

Personally, my problem with REs is that their is inadequate debug
capabilites for them. The best way I have found is to shove some
example text into Kodos and incrementally refine my RE by viewing the
RE's results  on the example text. RE's also tend to look like line
noise, even when you use the x switch and use whitesspace+comments.

> Python Culture says: 'Explicit is better than implicit'. May it be
> related to this?
Well, by putting RE's into the language I presume you also mean doing
the equivalent of setting group variables? That would be a 'magic' side
effect unless you explicitly assigned them, e.g:

 person, craving = r"My\s(\S+)likes\s(\S+)" ~~ text
# using ~~ for an RE match operator

The more readable I try and make it though, the more I appreciate the
Python way of doing things. 

- Pad.




More information about the Python-list mailing list