正则表达式仅指定特定的命中序列

regex sbustitute only specific hit sequence

我有多个字符串变体:"gr_shoulder_r_tmp""r_shoulder_tmp" 我需要替换:

"r_"l_,此处:

"gr_shoulder_r_tmp" > "gr_shoulder_l_tmp"
"r_shoulder_tmp" > "l_shoulder_tmp"

换句话说,我需要用第一个例子中的第三个共同点代替 和 stirngs 的第二个例子中的第一个

我开始挖掘自己... 并得出了一半解决的结果,这带来了一个更有趣的问题:

a) Find index of right hit

[i for i, x in enumerate(re.findall("(.?)(r_)", "gr_shoulder_r_tmp")) if filter(None, x).__len__() == 1] 这给了我 indx = 2

?) how to use that hit index :[

写这篇文章时,我找到了直接简单的解决方案..

b) split by underscore, replace standalone letter, and join back

findtag = "r"
newtag = "l"
itemA = "gr_shoulder_r_tmp"
itemB = "r_shoulderr_tmp"
spl_str = itemA.split("_")
hit = spl_str.index(findtag)
spl_str[hit] = newtag
new_item = "_".join(spl_str)

itemAitemB都给了我我需要的东西..但我不喜欢它,太重太粗糙

一个简单的正则表达式就可以完成这项工作。

re.sub(r'(?<![a-zA-Z])r_', 'l_', s)

(?<![a-zA-Z]) 负后视,它断言匹配之前将有任何但不是字母。

示例:

>>> re.sub(r'(?<![a-zA-Z])r_', 'l_',"gr_shoulder_r_tmp")
'gr_shoulder_l_tmp'
>>> re.sub(r'(?<![a-zA-Z])r_', 'l_',"r_shoulder_tmp")
'l_shoulder_tmp'