Contents

20.2.4. 多行匹配

import re
text1 = '/* this  is a comment */'
text2 = ''' this  is a
mulition   commint'''

comment = re.compile(r'/\*(.*?)\*/')
print(comment.findall(text1))
print(comment.findall(text2))

输出信息

[' this  is a comment ']
[]
# re.DOTALL ,在这里非常有用。 它可以让正则表达式中的点(.)匹配包括换行符在内的任意字符
comment = re.compile(r'/\*(.*?)\*/', re.S)
# comment = re.compile(r'/\*(.*?)\*/', re.DOTALL)

print(comment.findall(text2))

输出信息

[' this is a\nmultiline comment ']