首页 > 解决方案 > 如果字符串前面有数字和冒号的模式

问题描述

我对 Python 还是很陌生,我一直在尝试研究如何使用 if 语句,如果特定字符串前面有某种模式,它看起来会是什么样子。

例如,特别是文本

Sep 09 07:54:28 INFO: line of text here

我在读取脚本的文件中有多行这样的行。每一行的日期和时间都会发生变化,因此我无法准确指定该文本。

我试图INFO用别的东西代替这个词。

但是,这个词INFO分散在整个文本文件中,我不想替换它的每个实例。

我只想替换INFO前面的number number, colon, number number, colon, number number.

所以我使用了if语句,string.replace(old, new)并且我一直在阅读“积极的后向断言”,例如。(?<=abc)def

但我不确定如何指定文本模式,而不是指定确切的文本。

只需要指出正确的方向!谢谢

编辑:我还应该指出,还有其他实例INFO以数字开头,所以我不想让规则简单地“以数字开头”。它需要特别是那种模式(xx:xx:xx)

EDIT2:在此处放置另一个示例以根据评论进一步澄清

Sep 09 07:54:28 INFO: line of text here that contains many words

line of text that also contains the word INFO in the same line

Sep 09 07:56:30 INFO: line of text here that also contains many words

121334234: line of text here that contains INFO as well

我想替换单词 INFO,但仅限于具有该格式时间的行(num,num,colon,num num,colon,num num)

编辑 3:

with open(infile) as f:
    f = f.read()

with open(infile, 'r') as IN, open('output.html', 'w') as OUT:
    f = re.sub(r'(?<=\d{2}:\d{2}:\d{2})\s*INFO\b', ' INFO2', f)

这没有返回任何错误,但没有执行任何操作

编辑4:

OUT.write(re.sub(r'(?<=\d{2}:\d{2}:\d{2})\s*INFO\b', ' INFO2', f))

现在这确实用 INFO2 替换了 INFO,但它也阻止了它下面的所有代码工作。但这取决于我放置代码的位置。如果我把它放在我所有其他代码之后,它似乎没有做任何事情,如果我把它直接放在我定义我的 IN 和 OUT 之后,那么它会破坏它下面代码的所有格式

标签: python

解决方案


您可以使用以下方法:

import re

s = '''Sep 09 07:54:28 INFO: line of text here that contains many words

line of text that also contains the word INFO in the same line

Sep 09 07:56:30 INFO: line of text here that also contains many words

121334234: line of text here that contains INFO as well'''
repl_str = 'new_info'   # sample replacement string

s = re.sub(r'(?<=\d{2}:\d{2}:\d{2})\s*INFO\b', f' {repl_str}', s)
print(s)

输出:

Sep 09 07:54:28 new_info: line of text here that contains many words

line of text that also contains the word INFO in the same line

Sep 09 07:56:30 new_info: line of text here that also contains many words

121334234: line of text here that contains INFO as well

推荐阅读