更新时间:2023-02-19 15:10:53
Ì 建议先提取第一个数字,然后在没有用 re.sub
的其他数字括起来的情况下增加所有出现的数字>:
Ì suggest first extracting the first number and then increment all occurrences of this number when it is not enclosed with other digits with re.sub
:
import re
a1 = 'images1subimages1/folder100/hello1.png'
num0_m = re.search(r'\d+', a1) # Extract the first chunk of 1+ digits
if num0_m: # If there is a match
rx = r'(?<!\d){}(?!\d)'.format(num0_m.group()) # Set a regex to match the number when not inside other digits
print(re.sub(rx, lambda x: str(int(x.group())+1), a1)) # Increment the matched numbers
# => images2subimages2/folder100/hello2.png
查看 Python 演示