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


Groups > comp.lang.python > #74108 > unrolled thread

What is 're.M'?

Started byrxjwg98@gmail.com
First post2014-07-07 07:08 -0700
Last post2014-07-07 17:39 +0100
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#74108 — What is 're.M'?

Fromrxjwg98@gmail.com
Date2014-07-07 07:08 -0700
SubjectWhat is 're.M'?
Message-ID<a35ecdcb-312a-4fb5-9822-209ea1823774@googlegroups.com>
Hi,

I learn this short Python code from:

http://www.tutorialspoint.com/python/python_reg_expressions.htm

but I still do not decipher the meaning in its line, even after read its command
explanation.

It says that:
re.M:

 Makes $ match the end of a line (not just the end of the string) and makes ^
 match the start of any line (not just the start of the string). 

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

I have tried several other modification to the searchObj line, without clue yet.


Could you explain re.M and the following two searchObj mechanisms?

Thanks,



import re

line = "Cats are smarter than dogs";


searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I)
# searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I)

if searchObj:
   print "searchObj.group() : ", searchObj.group()
   print "searchObj.group(1) : ", searchObj.group(1)
   print "searchObj.group(2) : ", searchObj.group(2)
else:
   print "Nothing found!!"

[toc] | [next] | [standalone]


#74109

FromSkip Montanaro <skip@pobox.com>
Date2014-07-07 09:15 -0500
Message-ID<mailman.11593.1404742536.18130.python-list@python.org>
In reply to#74108
Scroll down to the "Module Contents" section of this page:

https://docs.python.org/2/library/re.html

It explains re.M and other "constants".

Skip

[toc] | [prev] | [next] | [standalone]


#74110

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-07-07 15:25 +0100
Message-ID<mailman.11594.1404743126.18130.python-list@python.org>
In reply to#74108
On 07/07/2014 15:08, rxjwg98@gmail.com wrote:
> Hi,
>
> I learn this short Python code from:
>
> http://www.tutorialspoint.com/python/python_reg_expressions.htm
>
> but I still do not decipher the meaning in its line, even after read its command
> explanation.
>
> It says that:
> re.M:
>
>   Makes $ match the end of a line (not just the end of the string) and makes ^
>   match the start of any line (not just the start of the string).
>
> More specific, what does 're.M' means?
>
> I have tried several other modification to the searchObj line, without clue yet.
>
> Could you explain re.M and the following two searchObj mechanisms?
>
> Thanks,
>
> import re
>
> line = "Cats are smarter than dogs";
>
> searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I)
> # searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I)
>
> if searchObj:
>     print "searchObj.group() : ", searchObj.group()
>     print "searchObj.group(1) : ", searchObj.group(1)
>     print "searchObj.group(2) : ", searchObj.group(2)
> else:
>     print "Nothing found!!"
>

The answer is on the first link that I sent you yesterday afternoon, 
that's 06/07/2014 15:25 BST.  Was the email not delivered or did you not 
bother to read it?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#74112

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-07 14:46 +0000
Message-ID<53bab2bb$0$29995$c3e8da3$5496439d@news.astraweb.com>
In reply to#74108
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

[toc] | [prev] | [next] | [standalone]


#74113

Fromrxjwg98@gmail.com
Date2014-07-07 07:59 -0700
Message-ID<0dcfeb67-7be3-48b9-a7d4-85e8202ea211@googlegroups.com>
In reply to#74112
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.

[toc] | [prev] | [next] | [standalone]


#74114

Fromrxjwg98@gmail.com
Date2014-07-07 08:16 -0700
Message-ID<5925191d-5219-4740-8039-b11ed3b00ccd@googlegroups.com>
In reply to#74112
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?

[toc] | [prev] | [next] | [standalone]


#74115

Fromrxjwg98@gmail.com
Date2014-07-07 08:20 -0700
Message-ID<fa56e308-cbee-46c3-87d7-8d735631c10e@googlegroups.com>
In reply to#74112
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,

[toc] | [prev] | [next] | [standalone]


#74125

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-07-07 17:39 +0100
Message-ID<mailman.11601.1404751209.18130.python-list@python.org>
In reply to#74115
On 07/07/2014 16:20, rxjwg98@gmail.com wrote:

For the second time, would you please use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web