Python查找并替换文件中的字符串,并带有以下参数:该字符串被引号而不是大字符串的一部分]

问题描述 投票:0回答:1
我需要解决以下问题。我想替换一个动态字符串(如果在文件中找到),但前提是该字符串旁边有引号,或者在该字符串旁边,或者最大之间有两个空格,并且不属于较大字符串(在python中):

ori = 'testing' rep = 'posting' file contents: 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 "

我希望使用正则表达式作为简单而有效的方式而不是单独的功能来寻找以下结果。

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 "

正则表达式魔术师可能会帮助我。

提前感谢。

我需要解决以下问题。我想替换一个动态字符串(如果在文件中找到),但前提是该字符串旁边有引号,或者在该字符串旁边,或者之间最多有两个空格,...

python regex regex-lookarounds
1个回答
0
投票
正则表达式将是完成此任务的好工具。始终要小心表达。正则表达式会很快变得令人费解并且难以调试。
© www.soinside.com 2019 - 2024. All rights reserved.