Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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,我想搜索并替换包含新行字符的文本块 在下面的示例中,当指定了DOTALL标志时,findall的行为与预期一致,。匹配包括换行符在内的任何字符。 但是当调用sub时,DOTALL标志似乎不起任何作用,并且找不到匹配项。我只想确认我不能用“.”替换包含新行字符的文本,或者如果我没有正确调用函数 代码 输出 我不太清楚原因,但您必须在中指定flags=(文档使用它) 这可能是因为可选的count参数 编辑: 我认为这毕竟是因为count参数,因为这同样有效: print 'this works:',

我想搜索并替换包含新行字符的文本块

在下面的示例中,当指定了DOTALL标志时,findall的行为与预期一致,
匹配包括换行符在内的任何字符。 但是当调用sub时,DOTALL标志似乎不起任何作用,并且找不到匹配项。我只想确认我不能用“.”替换包含新行字符的文本,或者如果我没有正确调用函数

代码 输出
我不太清楚原因,但您必须在中指定
flags=
(文档使用它)

这可能是因为可选的
count
参数

编辑:

我认为这毕竟是因为
count
参数,因为这同样有效:

print 'this works:', re.sub('START.*END', 'NEWTEXT', text, 0, re.DOTALL)

0
意味着全部替换。

打印“this works:”,re.sub('START.*END','NEWTEXT',text,0,re.DOTALL)#works@MarwanAlsabbagh是的,我刚刚测试了一下
this works: ['START\nbla bla\nbla bla\nEND']
this fails:
some example text...
START
bla bla
bla bla
END
print 'this works:', re.sub('START.*END', 'NEWTEXT', text, flags=re.DOTALL)
print 'this works:', re.sub('START.*END', 'NEWTEXT', text, 0, re.DOTALL)