您的位置:首页 > 编程语言 > Python开发

Python 正则表达式里的单行s和多行m模式

2015-09-08 22:43 447 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/pilicurg/article/details/48299675

Python 的re模块内置函数几乎都有一个flags参数,以位运算的方式将多个标志位相加。其中有两个模式:单行(re.DOTALL, 或者re.S)和多行(re.MULTILINE, 或者re.M)模式。它们初看上去不好理解,但是有时又会非常有用。这两个模式在PHP和JavaScripts里都有。

单行模式 re.DOTALL

在单行模式里,文本被强制当作单行来匹配,什么样的文本不会被当作单行?就是里面包含有换行符的文本,比如:

This is the first line.\nThis is the second line.\nThis is the third line.

正则表达式中,点号(.)能匹配除换行符外的所有字符,当搜索碰到换行符时,停止匹配。现在我们希望能匹配出包括换行符在内的整个字符串。例如:

>>> 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
>>> 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.']
>>>

更本质的说法是

多行模式改变了^和$的匹配行为

因为一个文件里经常有很多换行符,当需要在一个文件里跨行匹配时,单行和多行模式尤其有用。

原文链接:http://www.lfhacks.com/tech/python-re-single-multiline

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: