我知道对于解析我应该理想地删除所有空格和换行符但我这样做只是为了快速修复我正在尝试的东西而且我无法弄清楚为什么它不起作用..我已经包装了不同的区域我的文档中的文本带有“####1”之类的包装器,我正在尝试基于此进行解析,但无论我尝试什么,它都无法正常工作,我认为我正在正确使用多行.. 任何建议都表示赞赏
这根本不返回任何结果:
string='
####1
ttteest
####1
ttttteeeestt
####2
ttest
####2'
import re
pattern = '.*?####(.*?)####'
returnmatch = re.compile(pattern, re.MULTILINE).findall(string)
return returnmatch
最佳答案
Multiline 并不意味着 .
将匹配行返回,这意味着 ^
和 $
仅限于行
re.M re.MULTILINE
When specified, the pattern character '^' matches at the beginning of the string and at the >beginning of each line (immediately following each newline); and the pattern character '$' >matches at the end of the string and at the end of each line (immediately preceding each >newline). By default, '^' matches only at the beginning of the string, and '$' only at the >end of the string and immediately before the newline (if any) at the end of the string.
re.S
或 re.DOTALL
使 .
甚至匹配新行。
来源
关于Python 正则表达式,多行匹配模式.. 为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3534507/