Python 正则表达式,多行匹配模式.. 为什么这不起作用? [英] Python regex, matching pattern over multiple lines.. why isn't this working?
问题描述
我知道对于解析,我应该理想地删除所有空格和换行符,但我这样做只是为了快速解决我正在尝试的事情,我不知道为什么它不起作用..我已经包装了不同的区域我的文档中的文本带有诸如####1"之类的包装器,并且我正在尝试基于此进行解析,但无论我尝试什么,它都无法正常工作,我想我正在正确使用多行......感谢任何建议
I know that for parsing I should ideally remove all spaces and linebreaks but I was just doing this as a quick fix for something I was trying and I can't figure out why its not working.. I have wrapped different areas of text in my document with the wrappers like "####1" and am trying to parse based on this but its just not working no matter what I try, I think I am using multiline correctly.. any advice is appreciated
这根本不返回任何结果:
This returns no results at all:
string='
####1
ttteest
####1
ttttteeeestt
####2
ttest
####2'
import re
pattern = '.*?####(.*?)####'
returnmatch = re.compile(pattern, re.MULTILINE).findall(string)
return returnmatch
推荐答案
Try re.findall(r"####(.*?)s(.*?)s####", string, re.DOTALL)
(当然也适用于 re.compile
).
Try re.findall(r"####(.*?)s(.*?)s####", string, re.DOTALL)
(works with re.compile
too, of course).
此正则表达式将返回包含节编号和节内容的元组.
This regexp will return tuples containing the number of the section and the section content.
对于您的示例,这将返回 [('1', 'ttteest'), ('2', '
ttest')]
.
For your example, this will return [('1', 'ttteest'), ('2', '
ttest')]
.
(顺便说一句:你的例子不会运行,对于多行字符串,使用 '''
或 """
)
(BTW: your example won't run, for multiline strings, use '''
or """
)
这篇关于Python 正则表达式,多行匹配模式.. 为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!