Which regex syntax mode?

Tim Peters tim_one at email.msn.com
Fri Sep 15 06:24:55 EDT 2000


[Tim Hammerquist]
> I come from Perl

Then you're off to a good start -- you're motivated <wink>.

> and was wondering with RegExp syntax mode was most like Perl's syntax?

Probably the awk variant, but forget you asked that:  the "regex" module has
been obsolete for years, and will probably go away someday.  Use the "re"
module instead:  its syntax is a nearly exact clone of Perl5's regexp
syntax.  So you'll feel much more at home with that.

You're in for a bit of culture shock, though.  There's nothing special about
regular expressions in Python:  they're just another kind of object,
supplied by just another module, and the language proper knows nothing about
them.  Native Python programmers use regexps about as often as native Perl
programmers actually use Perl OO <0.9 wink>.

So get familiar with the "string" module soon.  People use that instead
whenever they can; it's considered clearer by everyone, and is generally
faster.  At the extreme, I've seen Perl like

    if ($var =~ /^a$/) { ... }

and that's just unthinkable here.  In Python everyone would write

    if var == "a":
        ...

instead.  Besides being clearer to the lazy Python eye, a real advantage is
that the Python spelling doesn't match "a\n" too by mistake <wink>.

tims-helping-tims-ly y'rs  - tim






More information about the Python-list mailing list