Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python equivalent to the Perl "/x" modifier for regular expressions?

Tags:

python

regex

perl

Perl makes it easy to construct readable regular expressions using the /x modifier. This modifier allows to write regular expression strings and ignore all whitespaces in these strings. In other words, logical parts of the regular expression can be separated by whitespace or even carriage returns, allowing great readability. In Python, the only way I see of doing this is to construct such regular expression string, remove whitespace from it in an intermediate step, and then use the resulting string for matching. Is there a more elegant way of doing this?

like image 589
Phonon Avatar asked Jun 19 '14 21:06

Phonon


People also ask

Does Python use Perl regex?

Python supports essentially the same regular expression syntax as Perl, as far as the regular expressions themselves. However, the syntax for using regular expressions is substantially different. Regular expression support is not available out of the box; you must import the re module.

What regex does Python use?

Python does not provide an inbuilt regex module. You need to install it using the pip command and then import it into your Python IDE. Then we stored some text in a variable named string.

Does Python have regex?

RegEx Module Python has a built-in package called re , which can be used to work with Regular Expressions.


1 Answers

Yes, by setting the re.X / re.VERBOSE flag:

This flag allows you to write regular expressions that look nicer. Whitespace within the pattern is ignored, except when in a character class, or when preceded by an unescaped backslash, or within tokens like *?, (?: or (?P<...>. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.

That means that the two following regular expression objects that match a decimal number are functionally equal:

a = re.compile(r"""\d +  # the integral part
                   \.    # the decimal point
                   \d *  # some fractional digits""", re.X)
b = re.compile(r"\d+\.\d*")

This is pretty much exactly like the /x Perl flag.

You can control the same flag in a subsection of your pattern within the (?x:...) (enable) and (?-x:...) (disable) groupings.

like image 129
Martijn Pieters Avatar answered Oct 12 '22 14:10

Martijn Pieters