Python正则表达式中的re.S

title: Python正则表达式中的re.S
date: 2014-12-21 09:55:54
categories: [Python]
tags: [正则表达式,python]
---

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

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

运行结果如下:

b is  []
c is  ['pass:
	234455
	']

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

原文地址:https://www.cnblogs.com/gide/p/5169274.html