Question

I have a string, and i need to check whether it contains a number/digit at the end of the string, and need to increment that number/digit at the end of the string with +1

I will get the strings as below

string2  = suppose_name_1
string3  = suppose_name_22
string4  = supp22ose45_na56me_45

for sure i will get the string in the above format like suppose_somthing + Underscore + digits

So from the above strings

  1. I need to check whether a string contains a number/digit at the end of the string after underscore
  2. If it contains then need to increment that with +1 like below

    string2 = suppose_name_2

    string3 = suppose_name_23

    string4 = supp22ose45_na56me_46

How can we do this in python by using regular expressions or something, but that should be very fast.

I have done something like here, but want to implement with re that will be very fast , so approached SO

Edit: sorry din't mentioned above

Sometimes it contains just something_name without integer, hence i need to check whether it contains a number in it first

Was it helpful?

Solution 2

You don't need regex. You can just use simple str.replace:

>>> s = 'suppose_name_1'
>>> index = s.rfind('_')  # Last index of '_'
>>> s.replace(s[index+1:], str(int(s[index+1:]) + 1))
'suppose_name_2'

If you need to first check whether you have digits at the end, you can check that using str.isdigit() method:

>>> s = 'suppose_name'
>>> 
>>> index = s.rfind('_')
>>> if s[index+1:].isdigit():
        s = s.replace(s[index+1:], str(int(s[index+1:]) + 1))


>>> s
'suppose_name'

OTHER TIPS

How about using regular expressions:

import re

def process_string(s):
    try:
        part1, part2 = re.search('^(.*_)(\d+)$', s).groups()
        part2 = str(int(part2) + 1)
        return part1 + part2 
    except AttributeError:
        return s

print process_string("suppose_name_1")
print process_string("suppose_name_22")
print process_string("supp22ose45_na56me_45")

print process_string("suppose_name")

prints:

suppose_name_2
suppose_name_23
supp22ose45_na56me_46
suppose_name

FYI, there is nothing wrong or scary with using regular expressions.

Here's short regex solution that increments the number with re.sub(...):

from re import sub

string2  = 'suppose_name_1'
string3  = 'suppose_name_22'
string4  = 'supp22ose45_na56me_45'
print [sub(r'^(?P<pretext>.*_)(?P<number>\d+)$', lambda x : '%s%d' % (x.group('pretext'), int(x.group('number')) + 1), s) for s in (string2, string3, string4)]

and the output:

['suppose_name_2', 'suppose_name_23', 'supp22ose45_na56me_46']

The easier to read version would be something like this:

from re import sub

string2  = 'suppose_name_1'
string3  = 'suppose_name_22'
string4  = 'supp22ose45_na56me_45'
regex = r'^(?P<pretext>.*_)(?P<number>\d+)$'

def increment(matchObject):
    return '%s%d' % (matchObject.group('pretext'), int(matchObject.group('number')) + 1)

for s in (string2, string3, string4):
    print sub(regex, increment, s)

and the output:

suppose_name_2
suppose_name_23
supp22ose45_na56me_46
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top