Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

regex - Python Regular Expression; replacing a portion of match

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.

Example input and output

e004_n07 #e4_n7
e020_n50 #e20_n50
e000_n00 #e0_n0

Can this be accomplished with re.sub alone, or do I need to use re.search/re.match?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Python demo:

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...