I'd like to increment the last digit of user provided string in Python 2.7.
I can replace the first digit like this:
def increment_hostname(name):
try:
number = re.search(r'\d+', name).group()
except AttributeError:
return False
number = int(number) + 1
number = str(number)
return re.sub(r'\d+', number, name)
I can match all the digits with re.findall then increment the last digit in the list but I'm not sure how to do the replace:
number = re.findall(r'\d+', name)
number = numbers[-1]
number = int(number) + 1
number = str(number)