builtin regular expressions?
Jorge Godoy
jgodoy at gmail.com
Sat Sep 30 08:37:49 EDT 2006
Antoine De Groote <antoine at vo.lu> writes:
> 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.
>
> Python Culture says: 'Explicit is better than implicit'. May it be related to
> this?
See if this isn't better to read:
================================================================================
def print_message(some_str):
if some_str.startswith('track='):
print "Your track is", some_str[6:]
elif some_str.startswith('title='):
print "It's a title of", some_str[6:]
elif some_str.startswith('artist='):
print "It was played by", some_str[7:]
else:
print "Oops, I dunno the pattern for this line..."
return
for line in ['track="My favorite track"', 'title="My favorite song"',
'artist="Those Dudes"', 'Something else']:
print_message(line)
================================================================================
Expected output:
================================================================================
Your track is "My favorite track"
It's a title of "My favorite song"
It was played by "Those Dudes"
Oops, I dunno the pattern for this line...
================================================================================
I came from Perl and was used to think with regular expressions for
everything. Now I rarely use them. They aren't needed to solve most of the
problems.
--
Jorge Godoy <jgodoy at gmail.com>
More information about the Python-list
mailing list