Python vs. Perl, which is better to learn?

Alex Martelli aleax at aleax.it
Wed May 1 03:08:22 EDT 2002


James J. Besemer wrote:
        ...
> The basic regex operators are similar to Python's, though Perl adds some
> extras such as
> 
>     {n,m}    # preceeding pattern matches at least n but no more than m
>     {times

This tends to confirm my impression that people who consider Perl's re's
preferable may not fully understand Python's:

>>> a35=re.compile('^a{3,5}$')
>>> print a35.match('aa')
None
>>> print a35.match('aaa')
<_sre.SRE_Match object at 0x8186698>
>>> print a35.match('aaaa')
<_sre.SRE_Match object at 0x8186950>
>>> print a35.match('aaaaa')
<_sre.SRE_Match object at 0x8186960>
>>> print a35.match('aaaaaa')
None
>>>

Python re's imitate Perl's closely enough that this is a rather unPythonic
arrangement -- the upper bound is *included* (Python's ranges, slices etc
have lower-bound-included, upper-bound-excluded...).


Alex




More information about the Python-list mailing list