Python在文件中查找并替换字符串,参数为该字符串被引用,而不是较大字符串的一部分
我需要解决以下问题。如果在文件中找到一个动态字符串,我想替换它,但前提是该字符串周围有引号,或者旁边有引号,或者中间有两个空格,并且不是较大字符串的一部分(在python中): 我希望下面的结果最好使用regex作为简单有效的方法,而不是单独的函数Python在文件中查找并替换字符串,参数为该字符串被引用,而不是较大字符串的一部分,python,regex,regex-lookarounds,Python,Regex,Regex Lookarounds,我需要解决以下问题。如果在文件中找到一个动态字符串,我想替换它,但前提是该字符串周围有引号,或者旁边有引号,或者中间有两个空格,并且不是较大字符串的一部分(在python中): 我希望下面的结果最好使用regex作为简单有效的方法,而不是单独的函数 Line1 This is one line with some words for testing purposes Line2 this is the seconds "testing" function. Line3 that is one m
Line1 This is one line with some words for testing purposes
Line2 this is the seconds "testing" function.
Line3 that is one more " testing" line
Line4 "posting"
Line5 " posting"
Line6 "posting "
Line7 " posting "
regex魔术师可能会在这方面帮助我
提前感谢。正则表达式将是执行此类任务的好工具。
始终注意清楚地表达它们。
正则表达式可能很快变得令人费解且难以调试
import re
original = 'testing'
replacement = 'posting'
line1 = 'This is one line with some words for testing purposes'
line2 = 'this is the seconds "testing" function.'
line3 = 'that is one more " testing" line'
line4 = '"testing"'
line5 = '" testing"'
line6 = '"testing "'
line7 = '" testing "'
lines = [line1, line2, line3, line4, line5, line6, line7]
starts_with_parentheses = '^"'
ends_with_parentheses = '"$'
one_space = ' {1}'
two_spaces = ' {2}'
none_one_or_two_spaces = '(|{}|{})'.format(one_space, two_spaces)
query = starts_with_parentheses \
+ none_one_or_two_spaces \
+ original \
+ none_one_or_two_spaces \
+ ends_with_parentheses
for line in lines:
match = re.search(query, line)
if match:
line = line.replace(original, replacement)
print(line)
产出:
This is one line with some words for testing purposes
this is the seconds "testing" function.
that is one more " testing" line
"posting"
" posting"
"posting "
" posting "
Line1
确实是文件中文本的一部分吗?为什么前两个“测试”和“测试”
实例没有被替换?它们不是较大字符串的一部分,引号和单词之间的空格少于3个。你说的“线”是指“线”吗?感谢您的澄清。Line1 Line2
不是行的一部分,只是标记。前两行没有改变,因为“testing”
是一个较大字符串的一部分,即“整行”。好的,引号实际上是文本文件的一部分吗?我建议删除行1
等,并准确显示文件中的内容。要求是模糊的,但一旦澄清,我可以发布一个解决方案,如果你不完全满意接受的一个。谢谢你马克斯克劳斯。这不适合line=re.sub
吗?re.sub似乎非常适合这项任务!我发现很难找到一种方法来用re.sub来代替“测试”和“发布”,同时保持周围的空间不变,但我相信这是可能的。再次感谢你,正则表达式对我来说仍然是一种黑魔法,我最终会到达那里。马克斯·克劳斯,你能不能用完整的文件读取来代替非常缓慢的逐行读取<使用open(filename,“r”)作为f:full\u read=f.read()
的code>,因此,不要在行中搜索,而是在full\u read
match=re.search(查询,full\u read)中进行搜索。如果匹配:full\u read=full\u read.replace(original,replacement)
那么,我认为用正则表达式进行替换可能是一个挑战。这是因为两件事需要不同的正则表达式,1。匹配不同的案例“测试”、“测试”、“测试”等。。。2.仅替换“testing”的字母,保留空格原样。也许可以用一个正则表达式来表达这一点,但我不知道有多抱歉。
This is one line with some words for testing purposes
this is the seconds "testing" function.
that is one more " testing" line
"posting"
" posting"
"posting "
" posting "