Files
Python_CookBook_repo/2.字符串和文本/8.编写多行模式的正则表达式.py

21 lines
966 B
Python
Raw Normal View History

2025-09-10 16:12:45 +08:00
import re
if __name__ == '__main__':
# 正常情况下,我们会想用.来匹配所有字符串,但这个东西不能匹配换行符
text1 = '/*This is a comment*/'
text2 = '''/*This is a
multiline comment */'''
comment = re.compile(r'/\*(.*?)\*/')
print(comment.findall(text1))
print(comment.findall(text2))
# 你会发现欸我靠咋匹配不到了,那是因为由于没办法识别\n,第二行被抛弃了
# 想要识别出\n,需要使用(?:.|\n)指定一个非捕获组,意思是在识别到/n的时候只做匹配但不触发捕获退出
comment_pro = re.compile(r'\*((?:.|\n)*?)\*/')
print(comment_pro.findall(text2))
# 对于这种简单的情况可以在compile函数中加入一个参数re.DOTALL来让.匹配包括\n在内的所有字符串
# PS复杂情况请另请高明
comment_pro_se = re.compile(r'\*(.*?)\*/', re.DOTALL)
print(comment_pro_se.findall(text2))