Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法按正则表达式找到多行_Python_Regex - Fatal编程技术网

Python 无法按正则表达式找到多行

Python 无法按正则表达式找到多行,python,regex,Python,Regex,我无法使用正则表达式在多行分组中找到匹配项 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) 预期结果:[“导入alkfa dfa aldkf”,导入我的名字是xyz] 实际结果:['import alkfa dfa aldkf

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

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)
预期结果:[“导入alkfa dfa aldkf”,导入我的名字是xyz]
实际结果:['import alkfa dfa aldkf']

您也可以在不使用多行标志的情况下使用此选项

re.findall(r'(import .+)\n*', text))

请删除新行字符后的空格\n,您就可以开始了

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.+'将找到以import end开头的字符串,该字符串包含除re.findall行之外的任何字符。

remove'^'