
Other Python articles: making an XML sitemap, languages easy to pick upįor daily tips on regular expressions, follow on Twitter. Notes on using regular expressions in other languages: PowerShell, C++, R, Mathematica To make a regular expression case-insensitive, pass the argument re.I (or re.IGNORECASE) as the final argument to re.search(). Setting this parameter to 1 causes only the first instance to be substituted, as in Perl’s s//. To change this behavior, you can specify the maximum number of instances to replace using the max parameter to re.sub(). Actually, re.sub() is analogous to s//g since it replaces all instances of a pattern by default. While we know about 171 links to Python, weve tracked only 3 mentions of Perl. To substitute for a pattern, analogous to Perl’s s// operator, use re.sub(). Based on our record, Python seems to be a lot more popular than Perl. If the match contains captured subexpressions, findall will return a list of tuples, the tuples being the captures. On the engineering side, Perl is fast and is unbeatable when it comes to text support. The findall method returns a list of matches rather than a match object. I prefer Perl (to Python) more for practical than for engineering reasons. To find all matches to a pattern, use re.findall() rather than re.search(). Python doesn’t have a global modifier like Perl’s /g option. The group method with a positive integer argument returns captured expressions: group(1) returns the first capture, group(2) returns the second, analogous to $1, $2, etc. The group method without any argument returns the entire match. You can retrieve captured matches via the group method on the match object. The functions match and search return None if no match is found and a match object otherwise. The function re.search() behaves like Perl’s m// and is probably what you want to use exclusively. It behaves as if every pattern has ^ prepended. It’s not! The re.match() function matches regular expressions starting at the beginning of a string. You might think that re.match() is the analog to Perl’s m// match operator.

To be safe, always use raw strings ( r'' or r"") to contain patterns. This means that some characters need to be escaped in order to be passed on to the regular expression engine.


Regular expression patterns are contained in strings, in contrast to Perl’s built-in // syntax. Regular expression support is not available out of the box you must import the re module. However, the syntax for using regular expressions is substantially different. Python supports essentially the same regular expression syntax as Perl, as far as the regular expressions themselves.
