Python 与不';前面没有其他文字
所以我有一个要求,我想用Python 与不';前面没有其他文字,python,regex,regex-lookarounds,Python,Regex,Regex Lookarounds,所以我有一个要求,我想用=替换字符串中的所有=,但问题是字符串可能包含=也是,我不想要=或=待替换。 所以仅仅用=替换=是行不通的,我在想是否有办法可以检查=是否没有然后将其替换。 我查找了look around regex,但这似乎并没有解决问题。使用: 输出: Hey this is a = test where != or = should not be changed 或 编辑: 您需要的是: =的正则表达式,其中=前面没有=: test = "Hey this is a = tes
=
替换字符串中的所有=
,但问题是字符串可能包含=代码>也是,我不想要=
或=代码>待替换。
所以仅仅用=
替换=
是行不通的,我在想是否有办法可以检查=
是否没有代码>然后将其替换。
我查找了look around regex,但这似乎并没有解决问题。使用:
输出:
Hey this is a = test where != or = should not be changed
或
编辑:
您需要的是:
=
的正则表达式,其中=
前面没有=
:
test = "Hey this is a = test where != or = should not be changed"
import re
print(re.findall(r'(?<!!)=', test))
test=“嘿,这是一个=测试,其中!=或=不应更改”
进口稀土
print(re.findall(r’)(?了解你的示例I/O?如果你只想替换=
,那么连续替换两个相等的值会有什么问题?重复问题。(?应该得到前面没有的相等值!
编辑了我的答案以进行修复,downvoter现在可能想删除他的否决票?
import re
replaced = re.sub('==', '=', test)
print(replaced)
test = "Hey this is a = test where != or = should not be changed"
import re
print(re.findall(r'(?<!!)=', test))