Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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,语言:python 我正在尝试匹配此段落中出现的所有内容,并将其从文件中删除 我想不出如何为它做正则表达式 Regex,那不行 ^#--- Maintenance ---#[\s\S]*[^#--- Maintenance ---#] 代码是什么样子的 #--- Maintenance ---# AuthType Basic AuthName "Restricted Content" AuthUserFile /home/fuelvnga/public_html/.htpasswd Requi

语言:
python

我正在尝试匹配此段落中出现的所有内容,并将其从文件中删除

我想不出如何为它做正则表达式

Regex,那不行

^#--- Maintenance ---#[\s\S]*[^#--- Maintenance ---#]
代码是什么样子的

#--- Maintenance ---#
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /home/fuelvnga/public_html/.htpasswd
Require valid-user
#--- Maintenance ---#

fsadfdsaf
dsaf
dsaf
sdaf
sda
fsa
f

#--- Maintenance ---#
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /home/fuelvnga/public_html/.htpasswd
Require valid-user
#--- Maintenance ---#


sdf safd sad

      #--- Maintenance ---#
      AuthType Basic
      AuthName "Restricted Content"
      AuthUserFile /home/fuelvnga/public_html/.htpasswd
      Require valid-user
      #--- Maintenance ---#
我需要突出显示下面的所有实例,即使它有缩进和其他奇怪的东西

<强>我想在评论上使用正则表达式,因为中间的内容可以改变< /强>:

#--- Maintenance ---#
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /home/fuelvnga/public_html/.htpasswd
Require valid-user
#--- Maintenance ---#
使用

这将用
替换匹配项。匹配是在注释
#--Maintenance-
#--Maintenance-#
以及注释之间的任何字符以非贪婪的方式开始/停止时完成的

诀窍是给标志
re.DOTALL
(简称:
re.S
),它将使点与换行符匹配。非贪婪很重要,它只在两条注释之间进行匹配,而不是在第一条注释的开始和最后一条注释的结束之间跨越所有文本

在代码中,我保留了multiline标志,但这个正则表达式不需要它

import re

text = """
#--- Maintenance ---#
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /home/fuelvnga/public_html/.htpasswd
Require valid-user
#--- Maintenance ---#

fsadfdsaf
dsaf
dsaf
sdaf
sda
fsa
f

#--- Maintenance ---#
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /home/fuelvnga/public_html/.htpasswd
Require valid-user
#--- Maintenance ---#


sdf safd sad

      #--- Maintenance ---#
      AuthType Basic
      AuthName "Restricted Content"
      AuthUserFile /home/fuelvnga/public_html/.htpasswd
      Require valid-user
      #--- Maintenance ---#

"""

matsch = re.sub(r"#--- Maintenance ---#(?:.*?)#--- Maintenance ---#", "",text,0, flags=re.M|re.S)
print (matsch)
u使用非贪婪匹配,该匹配应匹配两个维护块之间的任何内容

输出:

fsadfdsaf
dsaf
dsaf
sdaf
sda
fsa
f




sdf safd sad

你试过在每一行修剪吗?因为段落的意思是每行都有额外的空格,即使这样也不起作用。我不能选择所有出现的地方,中间的部分可以是任何东西。你想要所有出现的评论或段落吗?与下面的答案相同是的,这就是我想要的,但是你是怎么做到的呢?试试这个
/\\\\\\\\\\\\\\\\\\\/
@robertmylne解释站操作性?@PatrickArtner perfect非常感谢你的帮助,我真的很感激:)@tejuc如果有更多的注释,这也会杀死他们之间的文本-而且你正在匹配贪婪,所以第一个和最后一个
/
(为什么
/
?)将被杀死。请随意发布您自己的答案并进行解释。
fsadfdsaf
dsaf
dsaf
sdaf
sda
fsa
f




sdf safd sad