Python正则表达式里的单行re.S和多行re.M模式python
Python 的re模块内置函数几乎都有一个flags参数,以位运算的方式将多个标志位相加。其中有两个模式:单行(re.DOTALL, 或者re.S)和多行(re.MULTILINE, 或者re.M)模式。它们初看上去很差理解,可是有时又会很是有用。这两个模式在PHP和JavaScripts里都有。正则表达式
单行模式 re.DOTALL函数
在单行模式里,文本被强制看成单行来匹配,什么样的文本不会被看成单行?就是里面包含有换行符的文本,好比:spa
This is the first line.\nThis is the second line.\nThis is the third line.ip
点号(.)能匹配全部字符,换行符例外。如今咱们但愿能匹配出整个字符串,当用点号(.)匹配上面这个字符串时,在换行符的地方,匹配中止。例如:字符串
>>> a = 'This is the first line.\nThis is the second line.\nThis is the third line.'import
>>> print ahack
This is the first line.im
This is the second line.co
This is the third line.
>>> import re
>>> p = re.match(r'This.*line.' ,a)
>>> p.group(0)
'This is the first line.'
>>>
在上面的例子里,即便是默认贪婪(greedy)的匹配,仍然在第一行的结尾初中止了匹配,而在单行模式下,换行符被看成普通字符,被点号(.)匹配:
>>> q = re.match(r'This.*line.', a, flags=re.DOTALL)
>>> q.group(0)
'This is the first line.\nThis is the second line.\nThis is the third line.'
点号(.)匹配了包括换行符在内的全部字符。因此,更本质的说法是
单行模式改变了点号(.)的匹配行为
多行模式 re.MULTILINE
在多行模式里,文本被强制看成多行来匹配。正如上面单行模式里说的,默认状况下,一个包含换行符的字符串老是被看成多行处理。可是行首符^和行尾符$仅仅匹配整个字符串的起始和结尾。这个时候,包含换行符的字符串又好像被看成一个单行处理。
在下面的例子里,咱们但愿能将三句话分别匹配出来。用re.findall( )显示全部的匹配项
>>> a = 'This is the first line.\nThis is the second line.\nThis is the third line.'
>>> print a
This is the first line.
This is the second line.
This is the third line.
>>> import re
>>> re.findall(r'^This.*line.$', a)
[]
>>>
默认点号不匹配换行符,咱们须要设置re.DOTALL。
>>> re.findall(r'^This.*line.$', a, flags=re.DOTALL)
['This is the first line.\nThis is the second line.\nThis is the third line.']
>>>
匹配出了整句话,由于默认是贪婪模式,用问号切换成非贪婪模式:
>>> re.findall(r'^This.*?line.$', a, flags=re.DOTALL)
['This is the first line.\nThis is the second line.\nThis is the third line.']
>>>
仍然是整句话,这是由于^和$只匹配整个字符串的起始和结束。在多行模式下,^除了匹配整个字符串的起始位置,还匹配换行符后面的位置;$除了匹配整个字符串的结束位置,还匹配换行符前面的位置.
>>> re.findall(r'^This.*?line.$', a, flags=re.DOTALL+re.MULTILINE)
['This is the first line.', 'This is the second line.', 'This is the third line.']
>>>
更本质的说法是
多行模式改变了^和$的匹配行为
本文转自:
https://www.lfhacks.com/tech/python-re-single-multiline