Find the index of the first digit in a string

PythonRegexString

Python Problem Overview


I have a string like

"xdtwkeltjwlkejt7wthwk89lk"

how can I get the index of the first digit in the string?

Python Solutions


Solution 1 - Python

Use re.search():

>>> import re
>>> s1 = "thishasadigit4here"
>>> m = re.search(r"\d", s1)
>>> if m:
...     print("Digit found at position", m.start())
... else:
...     print("No digit in that string")
... 
Digit found at position 13

Solution 2 - Python

Here is a better and more flexible way, regex is overkill here.

s = 'xdtwkeltjwlkejt7wthwk89lk'

for i, c in enumerate(s):
    if c.isdigit():
        print(i)
        break

output:

15

To get all digits and their positions, a simple expression will do

>>> [(i, c) for i, c in enumerate('xdtwkeltjwlkejt7wthwk89lk') if c.isdigit()]
[(15, '7'), (21, '8'), (22, '9')]

Or you can create a dict of digit and its last position

>>> {c: i for i, c in enumerate('xdtwkeltjwlkejt7wthwk89lk') if c.isdigit()}
{'9': 22, '8': 21, '7': 15}

Solution 3 - Python

Thought I'd toss my method on the pile. I'll do just about anything to avoid regex.

sequence = 'xdtwkeltjwlkejt7wthwk89lk'
i = [x.isdigit() for x in sequence].index(True)

To explain what's going on here:

  • [x.isdigit() for x in sequence] is going to translate the string into an array of booleans representing whether each character is a digit or not
  • [...].index(True) returns the first index value that True is found in.

Solution 4 - Python

import re
first_digit = re.search('\d', 'xdtwkeltjwlkejt7wthwk89lk')
if first_digit:
    print(first_digit.start())

Solution 5 - Python

Seems like a good job for a parser:

>>> from simpleparse.parser import Parser
>>> s = 'xdtwkeltjwlkejt7wthwk89lk'
>>> grammar = """
... integer := [0-9]+
... <alpha> := -integer+
... all     := (integer/alpha)+
... """
>>> parser = Parser(grammar, 'all')
>>> parser.parse(s)
(1, [('integer', 15, 16, None), ('integer', 21, 23, None)], 25)
>>> [ int(s[x[1]:x[2]]) for x in parser.parse(s)[1] ]
[7, 89]

Solution 6 - Python

To get all indexes do:

idxs = [i for i in range(0, len(string)) if string[i].isdigit()]

Then to get the first index do:

if len(idxs):
    print(idxs[0])
else:
    print('No digits exist')

Solution 7 - Python

In Python 3.8+ you can use re.search to look for the first \d (for digit) character class like this:

import re

my_string = "xdtwkeltjwlkejt7wthwk89lk"

if first_digit := re.search(r"\d", my_string):
    print(first_digit.start())

Solution 8 - Python

I'm sure there are multiple solutions, but using regular expressions you can do this:

>>> import re
>>> match = re.search("\d", "xdtwkeltjwlkejt7wthwk89lk")
>>> match.start(0)
15

Solution 9 - Python

Here is another regex-less way, more in a functional style. This one finds the position of the first occurrence of each digit that exists in the string, then chooses the lowest. A regex is probably going to be more efficient, especially for longer strings (this makes at least 10 full passes through the string and up to 20).

haystack = "xdtwkeltjwlkejt7wthwk89lk"
digits   = "012345689"
found    = [haystack.index(dig) for dig in digits if dig in haystack]
firstdig = min(found) if found else None

Solution 10 - Python

As the other solutions say, to find the index of the first digit in the string we can use regular expressions:

>>> s = 'xdtwkeltjwlkejt7wthwk89lk'
>>> match = re.search(r'\d', s)
>>> print match.start() if match else 'No digits found'
15
>>> s[15] # To show correctness
'7'

While simple, a regular expression match is going to be overkill for super-long strings. A more efficient way is to iterate through the string like this:

>>> for i, c in enumerate(s):
...     if c.isdigit():
...         print i
...         break
... 
15

In case we wanted to extend the question to finding the first integer (not digit) and what it was:

>>> s = 'xdtwkeltjwlkejt711wthwk89lk'
>>> for i, c in enumerate(s):
...     if c.isdigit():
...         start = i
...         while i < len(s) and s[i].isdigit():
...             i += 1
...         print 'Integer %d found at position %d' % (int(s[start:i]), start)
...         break
... 
Integer 711 found at position 15

Solution 11 - Python

you can use regular expression

import re
y = "xdtwkeltjwlkejt7wthwk89lk"

s = re.search("\d",y).start()

Solution 12 - Python

import re
result = "  Total files:...................     90"
match = re.match(r".*[^\d](\d+)$", result)
if match:
    print(match.group(1))

will output

90

Solution 13 - Python

def first_digit_index(iterable):
    try:
        return next(i for i, d in enumerate(iterable) if d.isdigit())
    except StopIteration:
        return -1

This does not use regex and will stop iterating as soon as the first digit is found.

Solution 14 - Python

instr = 'nkfnkjbvhbef0njhb h2konoon8ll'
numidx = next((i for i, s in enumerate(instr) if s.isdigit()), None)
print(numidx)

Output:

12

numidx will be the index of the first occurrence of a digit in instr. If there are no digits in instr, numidx will be None.

I didn't see this solution here, and thought it should be.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAP257View Question on Stackoverflow
Solution 1 - PythonbgporterView Answer on Stackoverflow
Solution 2 - PythonAnurag UniyalView Answer on Stackoverflow
Solution 3 - PythonalukachView Answer on Stackoverflow
Solution 4 - PythonChristianView Answer on Stackoverflow
Solution 5 - PythonPaulo ScardineView Answer on Stackoverflow
Solution 6 - PythonSteven EckhoffView Answer on Stackoverflow
Solution 7 - PythonBoris VView Answer on Stackoverflow
Solution 8 - PythonMassimiliano TorromeoView Answer on Stackoverflow
Solution 9 - PythonkindallView Answer on Stackoverflow
Solution 10 - PythonmoinudinView Answer on Stackoverflow
Solution 11 - PythondrhanlauView Answer on Stackoverflow
Solution 12 - Pythonmichael tobackView Answer on Stackoverflow
Solution 13 - PythonJacobo de VeraView Answer on Stackoverflow
Solution 14 - PythonZackView Answer on Stackoverflow