8

So my copy of the classic book, Mastering Regular Expressions, just arrived, and I'm scanning through it. The cover (third edition) says, "for Perl, PHP, Java, .NET, Ruby, and More!" Well, it does have a full chapter for each of Perl, PHP, Java, and .NET - but no chapter on Ruby, or, more importantly to me, on Python.

Although the index lists a few points of Python's differences (such as how it handles newlines), there's not even a table of them, much less an entire chapter.

Which of the four covered languages, is Python's regex engine most similar to? More importantly, how similar, and what are the major differences?

Note - I don't mean how regexes are used in Python (versus, say Perl) - I know and use the re module. I'm only interested in the syntax differences of the actual regexes.

1
  • 5
    Python's own documentation for the re module claims it is similar to Perl's regular expression functionality, but as tchrist explains in this pretty extensive answer over at SO (answer here), the default re module is deficient in key areas like Unicode matching, and one should use the currently 3rd party regex module instead, if possible, as it has better functionality and Perl-compatibility. Ruby is also Perl-inspired, but also lacks a lot of functionality for things like Unicode.
    – wkl
    May 24, 2012 at 2:05

2 Answers 2

6

This site has a table comparing regex features for a wide range of languages and platforms (make sure to scroll to the bottom). There's also a page specific to Python, with more info about the re module (though, for a more complete regex library in Python, you should also look at the newer regex module).

1

Definitely check out the re module in Python. It might be (soon be?) outdated, but it's a great starting point for using regex in Python.

In my opinion, if you really want to get a handle on regular expressions, learn some Perl syntax. Regex is one of Perl's killer features, since regex is handled natively. They go hand in hand. Regex syntax is more or less the same across languages, particularly the basics like wildcards, anchors, special characters and substitution. Trust me when I say you can get pretty far on those fundamentals, but the more one knows, the better.

One of the main differences between Perl regex and the re module is that Python's approach is more object-oriented. Where in Perl you might do something procedural like this

if ($line =~ m/^Forty-two is the answer to [life|universe|everything]/)
{
   print "Don't Panic"
}

In Python you work with re objects. Something like this:

m = re.compile("^Forty-two is the answer to [life|universe|everything]")
if m.match(line): 
    print "Dont Panic"

The object oriented approach and the non-nativity are a bit tough to get used to if you've worked extensively with Perl's regex syntax but I find that after getting used to it, Python does a really good job with regular expressions.

2
  • Thanks, but I'm not asking about how to use Python's regex engine, only about the syntax of the actual regexes.
    – Cyclops
    May 24, 2012 at 21:12
  • @Cyclops Ah, I see. I misunderstood. The only resource I can provide for looking that syntactic differences among flavours of regex is twitter.com/#!/RegexTip May 25, 2012 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.