builtin regular expressions?

Jorge Godoy jgodoy at gmail.com
Sat Sep 30 11:50:45 EDT 2006


Mirco Wahab <wahab at chemie.uni-halle.de> writes:

> I don't see the point here, this example can be
> translated amost 1:1 to Perl and gets much more
> readable in the end, consider:

I could make it shorter in Python as well.  But for a newbie that haven't seen
the docs for strings in Python I thought the terse version would be more
interesting. 

At least he'll see that there are methods to do what he wants already builtin
with the language.

> sub print_message {
>    if   (/^(track=)/ ){ print 'Your track is '   .substr($_, length $1)."\n" }
>    elsif(/^(title=)/ ){ print 'It\'s a title of '.substr($_, length $1)."\n" }
>    elsif(/^(artist=)/){ print 'It was played by '.substr($_, length $1)."\n" }
>    else               { print "Oops, I dunno the pattern for this line...\n" }
> }
>
> print_message for ( 'track="My favorite track"', 'title="My favorite song"',
>                     'artist="Those Dudes"',      'Something else' );

If I were writing in Perl I'd not use substr like this and would write code
similar to the one the OP posted (i.e., /^track=(.*)/).

> OK, I do Perl and Python side by side and didn't reach
> that point so far, maybe beause I read the Friedel-Book
>  ( http://www.oreilly.com/catalog/regex2/reviews.html )
> sometimes and actually *like* the concept of regular expressions.

I like them as well.  I just don't see the need to use them everywhere. :-) 


-- 
Jorge Godoy      <jgodoy at gmail.com>



More information about the Python-list mailing list