The Python's regex different from Perl's , I want know what's the different?

Shawn Milochik Shawn at Milochik.com
Thu Feb 26 16:35:44 CET 2009


The regular expression syntax is basically exactly the same. The main
difference is that a regex can't just be written into a statement as
part of the syntax; you have to create a regular expression object and
use its methods.

So, instead of:
    new_thing =~ s/pattern/replacement/
it's:
    myPattern = re.compile(r'pattern')
    new_thing = myPattern.sub(replacement, input_string)
or:
    new_thing = re.sub(pattern, replacement, input_string)

Also, backreferences are \1 instead of $1, and you have to escape the
backslash or use a raw string for the backslash.

I've found this page to be pretty helpful:
http://www.regular-expressions.info/python.html

Shawn



More information about the Python-list mailing list