Python正则表达式;替换匹配的一部分 [英] Python Regular Expression; replacing a portion of match
问题描述
如何限制匹配/替换 e004_n07 中的前导零?但是,如果任一术语包含全零,那么我需要在该术语中保留一个零(请参见下面的示例).对于输入字符串,第一个值总是有 3 位数字,第二个值总是有 2 位数字.
How would I limit match/replacement the leading zeros in e004_n07? However, if either term contains all zeros, then I need to retain one zero in the term (see example below). For the input string, there will always be 3 digits in the first value and 2 digits in the second value.
示例输入和输出
e004_n07 #e4_n7
e020_n50 #e20_n50
e000_n00 #e0_n0
这可以单独使用 re.sub 来完成,还是需要使用 re.search/re.match?
Can this be accomplished with re.sub alone, or do I need to use re.search/re.match?
推荐答案
如果只想去掉字母后的零,可以使用:
If you want to only remove zeros after letters, you may use:
([a-zA-Z])0+
替换为 \1
反向引用.请参阅正则表达式演示.
Replace with \1
backreference. See the regex demo.
([a-zA-Z])
将捕获一个字母,0+
将匹配 1 个或多个零.
The ([a-zA-Z])
will capture a letter and 0+
will match 1 or more zeros.
import re
s = 'e004_n07'
res = re.sub(r'([a-zA-Z])0+', r'\1', s)
print(res)
请注意,re.sub
将查找并替换所有不重叠的匹配项(将执行全局搜索和替换).如果没有匹配项,字符串将按原样返回,无需修改.因此,无需使用额外的 re.match
/re.search
.
Note that re.sub
will find and replace all non-overlapping matches (will perform a global search and replace). If there is no match, the string will be returned as is, without modifications. So, there is no need using additional re.match
/re.search
.
UDPATE
如果数字只包含零,要保留 1 个零,您可以使用
To keep 1 zero if the numbers only contain zeros, you may use
import re
s = ['e004_n07','e000_n00']
res = [re.sub(r'(?<=[a-zA-Z])0+(\d*)', lambda m: m.group(1) if m.group(1) else '0', x) for x in s]
print(res)
查看 Python 演示
这里,r'(?<=[a-zA-Z])0+(\d*)'
正则表达式匹配一个或多个零 (0+
) 在一个 ASCII 字母 ((?<=[a-zA-Z])
) 之后,然后任何其他数字(0 或更多)被捕获到组 1 中,(\d*)
.然后,在替换中,我们检查Group 1是否为空,如果为空,我们插入0
(只有零),否则插入Group 1的内容(后面的剩余数字)第一个前导零).
Here, r'(?<=[a-zA-Z])0+(\d*)'
regex matches one or more zeros (0+
) that are after an ASCII letter ((?<=[a-zA-Z])
) and then any other digits (0 or more) are captured into Group 1 with (\d*)
. Then, in the replacement, we check if Group 1 is empty, and if it is empty, we insert 0
(there are only zeros), else, we insert Group 1 contents (the remaining digits after the first leading zeros).
这篇关于Python正则表达式;替换匹配的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!