1

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

Community
  • 1
  • 1
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • You said that your strings will be for sure in that format? Then why should you check whether the string ends with digits or not? – Rohit Jain Aug 07 '13 at 07:23

3 Answers3

2

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

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'
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • k rohit, but sorry i dint mentioned in the question sometimes i will get only suppose_name without number at the end, so first i need to check whether it contains integer in it. – Shiva Krishna Bavandla Aug 07 '13 at 07:31
1

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
Akinakes
  • 657
  • 4
  • 10