Correct Python expressions allow inline parameters?

In particular, I would like to know if I can specify a built-in option in the template line that will enable multi-line mode. That is, as a rule, with multi-line regex mode, Python is activated as follows:

pattern = re.compile(r'foo', re.MULTILINE) 

I would like to get a multi-line match by specifying it in the template line, instead of using the re.MULTILINE option. You can do this in Java with a built-in expression (? M). eg.

 pattern = re.compile(r'(?m)foo') 

Is this possible in Python, or do I need to use the re.M option? And anyway, is there a good link for inline template options in Python?

+4
source share
1 answer

Yes.

From docs :

(?iLmsux) (one or more letters from the set "i", 'L', 'm', 's', 'u', 'x'.)

The group corresponds to an empty string; letters, set the appropriate flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent) and re.X (verbose ), for all regex. (The flags are described in Module Content .)

This is useful if you want to include flags as part of the regular expression, instead of passing the flag argument to the compile() function.

Note that the flag (?x) changes the expression being parsed. Must be used first in an expression string, or after one or more spaces of characters. If there are characters without spaces before the flag, the results are undefined.

+6
source

All Articles