Python re.S【转】

来源:互联网 发布:c语言 逆波兰式计算器 编辑:程序博客网 时间:2024/04/30 23:40

转载自:http://www.myext.cn/other/a_29426.html 感谢博主!


在Python的正则表达式中,有一个参数为re.S。它表示多行匹配。看如下代码:


import rea = '''asdfsafhellopass:234455worldafdsf'''b = re.findall('hello(.*?)world',a)c = re.findall('hello(.*?)world',a,re.S)print 'b is ' , bprint 'c is ' , c

运行结果如下:


b is  []c is  ['pass:\n\t234455\n\t']

在字符串a中,包含换行符\n,在这种情况下,如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始。而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,在整体中进行匹配
0 0
原创粉丝点击