简体   繁体   English

检查字符串是否包含python中的数字/数字/数字

[英]Check whether a string contains a numeric/digit/number in python

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 我有一个字符串,我需要检查它是否包含字符串number/digit at the endnumber/digit at the end ,并需要在字符串number/digit at the end增加该number/digit +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 我肯定会得到字符串像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 如果它包含然后需要增加+1如下所示

    string2 = suppose_name_ 2 string2 = suppose_name_ 2

    string3 = suppose_name_ 23 string3 = suppose_name_ 23

    string4 = supp22ose45_na56me_ 46 string4 = supp22ose45_na56me_ 46

How can we do this in python by using regular expressions or something, but that should be very fast. 我们如何通过使用正则表达式或其他东西在python中执行此操作,但这应该非常快。

I have done something like here , but want to implement with re that will be very fast , so approached SO 我已经做了类似这里 ,但想重新实现,这将是非常快的,所以SO走近

Edit: sorry din't mentioned above Edit:抱歉,上面没有提到

Sometimes it contains just something_name without integer, hence i need to check whether it contains a number in it first 有时它只包含没有整数的something_name ,因此我需要先检查它是否包含一个数字

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. 仅供参考,使用正则表达式没有任何错误或可怕。

You don't need regex. 你不需要正则表达式。 You can just use simple str.replace : 你可以使用简单的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: 如果您需要首先检查最后是否有数字,可以使用str.isdigit()方法检查:

>>> 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'

Here's short regex solution that increments the number with re.sub(...): 这是一个简短的正则表达式解决方案,使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM