What is 're.M'?
rxjwg98 at gmail.com
rxjwg98 at gmail.com
Mon Jul 7 11:16:13 EDT 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
Thanks, your post makes me much clearer. But for my original example, it has
only one line: matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
re.M does not make any difference, is it right?
More information about the Python-list
mailing list