Python Regular Expression; Replacing A Portion Of Match
Solution 1:
If you want to only remove zeros after letters, you may use:
([a-zA-Z])0+
Replace with \1 backreference. See the regex demo.
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)
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
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)
See the Python demo
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).
Solution 2:
There's no need to use re.sub if your replacement is so simple - simply use str.replace:
s = 'e004_n07'
s.replace('0', '') # => 'e4_n7'Solution 3:
If your requirement is that you MUST use regex, then below is your regex pattern:
>>>import re>>>s = 'e004_n07'>>>line = re.sub(r"0", "", s)>>>line
'e4_n7'
However it is recommended not to use regex when there is other efficient way to perform the same opertaion, i.e. using replace function
>>>line = s.replace('0', '')>>>line
'e4_n7'
Post a Comment for "Python Regular Expression; Replacing A Portion Of Match"