如何用python re.sub仅替换部分匹配 [英] How to replace only part of the match with python re.sub
问题描述
我需要用一个 reg 表达式匹配两个 case 并进行替换
I need to match two cases by one reg expression and do replacement
'long.file.name.jpg' -> 'long.file.name_suff.jpg'
'long.file.name.jpg' -> 'long.file.name_suff.jpg'
'long.file.name_a.jpg' -> 'long.file.name_suff.jpg'
'long.file.name_a.jpg' -> 'long.file.name_suff.jpg'
我正在尝试执行以下操作
I'm trying to do the following
re.sub('(\_a)?.[^.]*$' , '_suff.',"long.file.name.jpg")
但这是删除扩展名.jpg",我得到了
But this is cut the extension '.jpg' and I'm getting
long.file.name_suff.而不是 long.file.name_suff.jpg我知道这是因为 [^.]*$ 部分,但我不能排除它,因为我必须找到最后一次出现的 '_a' 来替换或最后一次 '.'
long.file.name_suff. instead of long.file.name_suff.jpg I understand that this is because of [^.]*$ part, but I can't exclude it, because I have to find last occurance of '_a' to replace or last '.'
有没有办法只替换匹配的一部分?
Is there a way to replace only part of the match?
推荐答案
re.sub(r'(?:_a)?.([^.]*)$', r'_suff.1', "long.file.name.jpg")
?:
开始一个不匹配的组(SO 答案),所以 (?:_a)
匹配 _a
但没有枚举它,下面的问号使它成为可选的.
?:
starts a non matching group (SO answer), so (?:_a)
is matching the _a
but not enumerating it, the following question mark makes it optional.
所以在英语中,这表示,匹配结尾 .<anything>
跟随(或不跟随)模式 _a
So in English, this says, match the ending .<anything>
that follows (or doesn't) the pattern _a
另一种方法是使用lookbehind(见这里).提到这个是因为它们非常有用,但我做了 15 年的 RE 才知道它们
Another way to do this would be to use a lookbehind (see here). Mentioning this because they're super useful, but I didn't know of them for 15 years of doing REs
这篇关于如何用python re.sub仅替换部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!