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

S

Shawn Milochik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,240
Latest member
เพิ่มไลค์|LikePro

Latest Threads

Top