每天CookBook之Python-028

  • 多行匹配
import re

comment = re.compile(r'/\*(.*?)\*/')
text1 = '/* this is a comment */'
text2 = '''/* this is a
              multiline comment */
        '''

print(comment.findall(text1))
print(comment.findall(text2))

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

comment = re.compile(r'/\*(.*?)\*/', re.DOTALL)

print(comment.findall(text2))
[' this is a comment ']
[]
[' this is a\n              multiline comment ']
[' this is a\n              multiline comment ']
posted @ 2016-07-13 21:47  4Thing  阅读(101)  评论(0编辑  收藏  举报