需要帮助python regex。 (用于文件比较程序)
解析参数以“+”,“ - ”和“'开头的文件。 ('+ < - SPACE->',' - < - SPACE->','< - SPACE - > < - SPACE->')。我需要用一些文字替换它。例子
我想将其替换为: -
虽然Gillespie的解决方案非常棒,但我会说为了灵活性使用正则表达式。
我在下面提供一些示例和用例
.
这采取的形式
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
你不需要正则表达式。尝试类似的东西:
with open("myfile.txt") as f:
for line in f:
if line[0] == "+":
print('<p class="blue">{}</p>'.format(line[1:].strip()))
elif line[0] == "-":
print('<p class="green">{}</p>'.format(line[1:].strip()))
else:
print('<p class="normal">{}</p>'.format(line))
见例子:https://repl.it/repls/SameMintyDigit
编辑:Per @ Felk建议提高可读性:
def printLine(color, line):
print(f'<p class="{color}">{line}</p>')
with open("myfile.txt") as f:
for line in f:
if line[0] == "+":
printLine("blue", line[1:].strip())
elif line[0] == "-":
printLine("green", line[1:].strip())
else:
printLine("normal", line)