正则表达式:如何匹配包含“\n”的字符串(换行)?
我正在尝试使用正则表达式从 SQL 导出文件转储数据。为了匹配帖子内容的字段,我使用“(?P
”。大多数情况下它工作正常,但如果该字段包含“\n”字符串,则正则表达式将不匹配。如何修改正则表达式以匹配它们?谢谢!
示例(我使用的是Python):
>>> re.findall("'(?P<content>.*?)'","'<p>something, something else</p>'")
['<p>something, something else</p>']
>>> re.findall("'(?P<content>.*?)'","'<p>something, \n something else</p>'")
[]
PS 似乎所有前面带有“\”的字符串都被视为转义字符。我如何告诉 regx 按原样对待它们?
I'm trying to dump data from a SQL export file with regular expression. To match the field of post content, I use '(?P<content>.*?)
'. It works fine most of the time, but if the field contains the string of '\n' the regular expression wouldn't match. How can I modify the regular expression to match them? Thanks!
Example(I'm using Python):
>>> re.findall("'(?P<content>.*?)'","'<p>something, something else</p>'")
['<p>something, something else</p>']
>>> re.findall("'(?P<content>.*?)'","'<p>something, \n something else</p>'")
[]
P.S. Seemingly all strings with '\' in the front are treated as escape characters. How can I tell regx to treat them as they are?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
DOTALL
选项:请参阅 这个。
You should use
DOTALL
option:See this.
您需要 Dotall 修饰符,以使点也匹配换行符。
请参阅在 docs.python.org 上
You need the Dotall modifier, to make the dot also match newline characters.
See it here on docs.python.org