我需要在字符串中找到最后出现的数字(不是单个数字),并替换为number+1
,例如:/path/testcase9.in
to /path/testcase10.in
。如何在python中更好或更有效地做到这一点?
这是我现在使用的:
reNumber = re.compile('(\d+)')
def getNext(path):
try:
number = reNumber.findall(path)[-1]
except:
return None
pos = path.rfind(number)
return path[:pos] + path[pos:].replace(number, str(int(number)+1))
path = '/path/testcase9.in'
print(path + " => " + repr(self.getNext(path)))