从fi中删除带有特殊字符的多行模式

2024-06-28 20:12:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从文件中删除多行模式。 例如:

  <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command>
  <?ignore <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command> ?> 

要删除的节以以下开头:

 <?ignore

结尾为:

 ?>

我想用regex来做这个。Python3.6.3

with open('graph.xml', 'r') as readXML:
    tempFile = readXML.read()
    patr = re.compile("<?ignore.*?>", re.MULTILINE)
    tempFile = re.sub(patr,"",tempFile)
    print(tempFile)

结果:

  <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command>
  <?
     <input name="some input" />
     <output name="some output" />
  </Command> ?> 

我想删除所有部分,而不仅仅是部分第一行。你知道吗


Tags: namereinputoutputtype结尾模式some
3条回答

?是regex中的可选量词,因此a?表示chara是可选的。要检测到这个字符,你需要逃逸它。你知道吗

尝试

<\?ignore.*\?>

可以使用此模式<\?ignore.+?\?>删除多行模式:

示例:

import re

str = """
  <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command>
  <?ignore <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command> ?> 
  """

print(re.sub(r'<\?ignore.+?\?>', '', str, flags=re.MULTILINE|re.DOTALL))

打印出来:

  <Command name="somecom" type="type" >
     <input name="some input" />
     <output name="some output" />
  </Command>

不要忘记使用标志,否则更换将不起作用:

flags=re.MULTILINE|re.DOTALL

您可以使用修饰符(?s)使点与换行符匹配,并转义问号\?使其逐字匹配。您可以使点开始方法非贪婪.*?

(?s)<\?ignore.*?\?>

Regex demo| Python demo

或者您可以使用一个重复模式来匹配一个不包含?>的行,使用一个负的前瞻:

<\?ignore\b.*\n(?!.*\?>)(?:.*\n)*.*\?>
  • <\?ignore\b.*\n匹配<?ignore后跟1+次任意字符,后跟换行符
  • (?!.*\?>)负面展望,断言右边的不是?>
  • (?:.*\n)*重复0+次匹配任何字符,除非换行符后跟换行符
  • .*\?>匹配0+次任意字符和?>

Regex demo| Python demo

相关问题 更多 >