python非贪婪,多行匹配正则表达式

一些regular的tips:

1 非贪婪flag

>>> re.findall(r”a(\d+?)”, “a23b”)
        ['2']
>>> re.findall(r”a(\d+)”, “a23b”)
        ['23']

注意比较这种情况:

>>> re.findall(r”a(\d+)b”, “a23b”)
        ['23']
>>> re.findall(r”a(\d+?)b”, “a23b”)
        ['23']

2 如果你要多行匹配,那么加上re.S和re.M标志
re.S:.将会匹配换行符,默认.不会匹配换行符

>>> re.findall(r”a(\d+)b.+a(\d+)b”, “a23b\na34b”)
        []
>>> re.findall(r”a(\d+)b.+a(\d+)b”, “a23b\na34b”, re.S)
        [('23', '34')]
>>>

re.M:^$标志将会匹配每一行,默认^和$只会匹配第一行

>>> re.findall(r”^a(\d+)b”, “a23b\na34b”)
        ['23']
>>> re.findall(r”^a(\d+)b”, “a23b\na34b”, re.M)
        ['23', '34']

但是,如果没有^标志,

>>> re.findall(r”a(\d+)b”, “a23b\na23b”)
        ['23', '23']

可见,是无需re.M

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

No related posts.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu