Python正则表达式匹配一行并替换

时间:2019-03-12 16:18:29

标签: python regex python-3.x replace regex-negation

需要有关python regex的帮助。 (用于文件比较程序)

以“ +”,“-”和“'”开头的参数解析文件。 ('+ <-SPACE->','-<-SPACE->','<-SPACE-> <-SPACE->')。我需要用一些文字替换它。例子

enter image description here

我想将其替换为:-

enter image description here

1 个答案:

答案 0 :(得分:1)

尽管Gillespie的解决方案很棒,但是为了灵活性起见,我还是要使用regex。

我在下面提供了一些示例和用例

此格式为

re.sub(r'[+\-\s]{,4}((?:YOUR_QUERY_HERE[\s]*[\S]+(?=[\s]+|$)))', r'\n<p class="blue">\1</p>', string)

YOUR_QUERY_HERE替换为您想要的内容

因此,您可以使用This[\s]*is[\s]*line[\s]*[\S]+

>>> import re

>>> string = '''+ This is line one
- This is line two
This is line three'''

>>> capture = re.sub(r'[+\-\s]{,4}?((?:This[\s]*is[\s]*line[\s]*[\S]+(?=[\s]+|$)))', r'\n<p class="blue">\1</p>', string)
>>> print(capture)

<p class="blue">This is line one</p>
<p class="blue">This is line two</p>
<p class="blue">This is line three</p>

如果文件中的行数不同,则可以像这样针对特定的组

>>> import re

>>> string = '''+ This is line one
- This is line two
  This is line three
+ Bobby has hobbies
- Meg is a brain
+ The pool hurts
  Manny is love
  This is line four
- The end of the world
- This is line five
+ This is line six
  Is upon us'''


>>> capture = re.sub(r'[+\-\s]{,4}?((?:This[\s]*is[\s]*line[\s]*[\S]+(?=[\s]+|$)))', r'\n<p class="blue">\1</p>', string)
>>> print(capture)

<p class="blue">This is line one</p>
<p class="blue">This is line two</p>
<p class="blue">This is line three</p>
+ Bobby has hobbies
- Meg is a brain
+ The pool hurts
  Manny is love
<p class="blue">This is line four</p>
- The end of the world
<p class="blue">This is line five</p>
<p class="blue">This is line six</p>
  Is upon us