首页 > 解决方案 > 无法通过正则表达式找到多行

问题描述

我无法使用正则表达式在组中的多行上找到匹配项

import re

text = "import alkfa dfa aldkf \n import my name is xyz \n i am 
working incoverfox"

m = re.findall(r'^(import .+?$)', text, re.MULTILINE)
print(m)

预期结果:['import alkfa dfa aldkf', import my name is xyz] 实际结果:['import alkfa dfa aldkf']

标签: pythonregex

解决方案


import re

text = "a import alkfa dfa aldkf \n import my name is xyz \n i am working incoverfox"
re.findall(r'(import .+)', text)

r'(import .+)'将找到以 end 开头的字符串,import除了\n.


推荐阅读