What is 're.M'?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jul 7 16:46:19 CEST 2014

On Mon, 07 Jul 2014 07:08:53 -0700, rxjwg98 wrote:

> More specific, what does 're.M' means?

Feel free to look at it interactively. re.M is a flag to control the 
meaning of the regular expression. It is short for re.MULTILINE, just as 
re.I is short for re.IGNORECASE:

py> import re
py> re.M == re.MULTILINE
py> re.I == re.IGNORECASE

They are just numeric flags:

py> re.I
py> re.M

so you can combine then:

py> re.I | re.M

re.M turns on "multi-line matching". This changes the meaning of the 
special characters ^ and $.

Standard mode:
	^ matches the start of the string
	$ matches the end of the string

Multi-line mode:
	^ matches the start of each line
	$ matches the end of each line

Here is an example. Copy and paste this into the interpreter:

import re
text = """First line.
Second line.
Third line."""
pattern = "^.*$"  # Match anything from the start to end.

By default, . does not match newlines, so by default the regex matches 

py> re.search(pattern, text) is None  # Nothing matches!

If you use MULTILINE mode, $ matches the end of the first line:

py> re.search(pattern, text, re.M).group()
'First line.'

If you add MULTILINE mode and DOTALL mode, it matches everything:

py> re.search(pattern, text, re.M|re.S).group()
'First line.\nSecond line.\nThird line.'

See the reference manual for more details:

(Python 3)

(Python 2)


More information about the Python-list mailing list