What is 're.M'?

rxjwg98 at gmail.com rxjwg98 at gmail.com
Mon Jul 7 17:20:42 CEST 2014


On Monday, July 7, 2014 10:46:19 AM UTC-4, Steven D'Aprano wrote:
> 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
> 
> True
> 
> py> re.I == re.IGNORECASE
> 
> True
> 
> 
> 
> 
> 
> They are just numeric flags:
> 
> 
> 
> py> re.I
> 
> 2
> 
> py> re.M
> 
> 8
> 
> 
> 
> so you can combine then:
> 
> 
> 
> 
> 
> py> re.I | re.M
> 
> 10
> 
> 
> 
> 
> 
> 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 
> 
> nothing:
> 
> 
> 
> 
> 
> py> re.search(pattern, text) is None  # Nothing matches!
> 
> True
> 
> 
> 
> 
> 
> 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:
> 
> 
> 
> https://docs.python.org/3/library/re.html#module-contents
> 
> (Python 3)
> 
> 
> 
> https://docs.python.org/2/library/re.html#module-contents
> 
> (Python 2)
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven


Your patter is: pattern = "^.*$" 
while my example has pattern: '(.*) are (.*?) .*'
which does not have either '^' or '$'

Why re.M has effects on my example? Thanks,




More information about the Python-list mailing list