How to get the total number of digits at the end of the string

Viewed 96

I have a list containing server names like ['oracle0123','oracle0124','oracle0125']. I want to check how many digits are at the end of the server name, as this varies (as in this case, it is 4). I have a vague idea how this should be done, but my approach didn't work.

v=['oracle0123','oracle0124','oracle0125']

def get_num_position(v):
    for i in v:
        i=i[::-1]
        print('reverse server is-',i)
        for j in i:
            x=0
            if j.isdigit():
                x = x+1
            print(x)
return x

get_num_position(v)
5 Answers

You could also use re.split to achieve this:

>>> import re
>>> s = "oracle1234ad123"
>>> first, _ = re.split("\d+$", s)
>>> len(s) - len(first)
3

Note that the code above will fail, if the input string does not end with the number:

>>> first, _ = re.split("\d+$", "foobar")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 2, got 1)

In Python 3 you could use the * assignment, to avoid such errors:

>>> first, *rest = re.split("\d+$", "foobar")
>>> first
'foobar'
>>> rest
[]

The problem is that you're resetting the value of x to 0 for every character. Also, I'm guessing you wanted to print x only after looping through each word. This should work without changing the logic of your code much:

v=['oracle0123','oracle0124','oracle0125']

def get_num_position(v):
    for i in v:
        i=i[::-1]
        print('reverse server is-',i)
        x=0
        for j in i:
            if j.isdigit():
                x = x+1
        print(x)

get_num_position(v)

This would work fine for you.

code

v=['oracle0123','oracle0124','oracle0125']

def get_num_position(v):
    count = []
    for i in v:
        tCount = 0
        for j in i:
            if j.isnumeric():
                tCount += 1
        print(tCount)
        count.append(tCount)
    return count
get_num_position(v)

Output:

4
4
4

Try:

import re

for i in v:
    match=re.search(r'\d+$',i)
    if match:
        print(i, len(match.group()))
    else:
        print(i, '0')

With a regular expression:

  • r'\d*$'
    • $ from the end
    • \d* find all the digits
  • re.search finds the pattern
  • .group returns the match group
  • len determines the length of the group
  • Assumes there is at least one number at the end
  • Using a List Comprehension
  • Examples have been provided with numbers
    • Only at the end
    • the beginning and end
    • the middle and the end
    • the beginning middle and end
  • The code only returns the len of numbers at the end
import re

values = ['oracle01234',
          'oracle012468',
          'oracle01258575',
          '0123oracle0123',
          '0123oracle0124555',
          '0123oracle01255',
          'ora0123cle01234',
          'or0123acle0124333333',
          'o0123racle01254',
          '123or0123acle0124333333']


count_of_end_numbers = [len(re.search(r'\d*$', x).group()) for x in values]

print(count_of_end_numbers)

>>> [5, 6, 8, 4, 7, 5, 5, 10, 5, 10]
Related