问题
你正在试着使用正则表达式去匹配一大块的文本,而你需要跨越多行去匹配。
解决方案
这个问题很典型的出现在当你用点(.)去匹配任意字符的时候,忘记了点(.)不能匹配换行符的事实。比如,假设你想试着去匹配C语言分割的注释:
>>> comment = re.compile(r'/\*(.*?)\*/')
>>> text1 = '/* this is a comment */'
>>> text2 = '''/* this is a
... multiline comment */
... '''
>>>
>>> comment.findall(text1)
[' this is a com