.(句点)匹配除了换行之外的所有一个字符, .*(点-星)匹配除了换行外的所有字符
>>>
>>> r=re.compile(r'.*')
>>> r.search('How are you\nFine thank you and you\nI am fine too').group()
'How are you'
>>>
这个例子可以看出.*(点-星)匹配除了换行外的所有字符,但无法匹配换行符,如何匹配包括换行符的所有字符呢?
1、通过传入re.DOTALL或者re.S作为re.compile()的第二个参数
>>>
>>> r=re.compile(r'.*',re.DOTALL)
>>> r.search('How are you\nFine thank you and you\nI am fine too').group()
'How are you\nFine thank you and you\nI am fine too'
>>> r=re.compile(r'.*',re.S)
>>> r.search('How are you\nFine thank you and you\nI am fine too').group()
'How are you\nFine thank you and you\nI am fine too'
>>>
2、通过 (.|\n)* 正则表达式来匹配所有字符
>>>
>>> r=re.compile(r'(.|\n)*')
>>> r.search('How are you\nFine thank you and you\nI am fine too').group()
'How are you\nFine thank you and you\nI am fine too'
>>>
除了re.DOTALL外,re.IGNORCASE(等价于re.I),re.MULTILINE(re.M),也是很有用的参数
re.IGNORCASE可以忽略大小写
>>>
>>> r=re.compile(r'hello',re.I)
>>> r.findall('Hello hello world heLLo')
['Hello', 'hello', 'heLLo']
>>>