I have a string of following types:
a1 = 'images1subimages1/folder100/hello1.png'
a1 = 'images1subimages1 folder100 hello1.png'
a1 = 'images1subimages1folder100hello1.png'
a1 = 'images1b100d1.png'
The first Integer of the string is num0 and we only care about it. We want to increase all occurrence of num0 by one and keep other numbers the same.
Required:
a2 = 'images2subimages2/folder100/hello2.png'
a2 = 'images2subimages2 folder100 hello2.png'
a2 = 'images2subimages2folder100hello2.png'
a2 = 'images2b100d2.png'
My attempt:
import re
a1 = 'images1subimages1/folder100/hello1.png'
nums = list(map(int, re.findall(r'\d+', a1)))
nums0 = nums[0]
nums_changed = [j+1 if j==nums[0] else j for i,j in enumerate(nums)]
parts = re.findall(r'(\w*\d+)',a1)
for i in range(len(parts)):
num_parts = list(map(int, re.findall(r'\d+', parts[i])))
for num_part in num_parts:
if num_part == nums0:
parts[i] = parts[i].replace(str(nums0), str(nums0+1))
ans = '/'.join(parts)
ans
This has the following result:
a1 = 'images1subimages1/folder100/hello1.png' # good
a1 = 'images1subimages1 folder100 hello1.png' # bad
Is there a general way to solve the problem using regex in python?