Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74113

Re: What is 're.M'?

Newsgroups comp.lang.python
Date 2014-07-07 07:59 -0700
References <a35ecdcb-312a-4fb5-9822-209ea1823774@googlegroups.com> <53bab2bb$0$29995$c3e8da3$5496439d@news.astraweb.com>
Message-ID <0dcfeb67-7be3-48b9-a7d4-85e8202ea211@googlegroups.com> (permalink)
Subject Re: What is 're.M'?
From rxjwg98@gmail.com

Show all headers | View raw


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 all of you. The email address associates with this group is not my 
everyday email. Excuse me for not reply yet.

I did read for several times the part you point out. I admit, that I have 
difficulties on reading now. In that explanation, it says about '$' and '^'.
But I do not see any '$' and '^' in "r'(.*) are (.*?) .*'"

'$' and '^' are pattern characters? It should be inside 

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)



How to connect '$' or '^' to:

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)


Thanks for your help and please correct me now.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is 're.M'? rxjwg98@gmail.com - 2014-07-07 07:08 -0700
  Re: What is 're.M'? Skip Montanaro <skip@pobox.com> - 2014-07-07 09:15 -0500
  Re: What is 're.M'? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-07 15:25 +0100
  Re: What is 're.M'? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-07 14:46 +0000
    Re: What is 're.M'? rxjwg98@gmail.com - 2014-07-07 07:59 -0700
    Re: What is 're.M'? rxjwg98@gmail.com - 2014-07-07 08:16 -0700
    Re: What is 're.M'? rxjwg98@gmail.com - 2014-07-07 08:20 -0700
      Re: What is 're.M'? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-07 17:39 +0100

csiph-web