[英]how to catch EOL with regular expression in python?
我有一个字符串,我想用 python 中的正则表达式捕获/匹配它,它是一个多行字符串,所以它在中间有 EOL,但是我的正则表达式在这种情况下不起作用并给出如下错误,任何人都知道如何处理? 谢谢!
import re
comment = re.compile(r'/((.|\n)*)/')
text="/hello!
/"
m=re.findall(comment, text)
if m:
我得到的错误:
文件 "", line 3 text="/hello!/ ^ SyntaxError: EOL while scan string literal
m=re.findall(comment, text, re.DOTALL)
允许.
匹配换行符(我认为这就是问题)
print(re.findall("\/.*?\/","""/hel
lo!/""",re.DOTALL))
# ['/hel\nlo!/']
print(re.findall("\/.*?\/","""/hel
lo111/garbage
asd more garbage /h
ello222/""",re.DOTALL))
# ['/hel\nlo111/', '/h\nello222/']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.