Home > Blockchain >  How to extract numbers from a string without regex?
How to extract numbers from a string without regex?

Time:06-25

I would like to extract all the numbers contained in a string. I can't use regex, is there any other way?

Example:userVariable = "someTextWithNumbers432andTextwith331"

Result:[432,331]

CodePudding user response:

Try this groupby from itertools. Please ask if you have any question.

from itertools import groupby
s = "your long string"

for k, g in groupby(s, key=lambda ch: ch.isdigit()):
    if k: print(''.join(list(g)))   # you can use result.append() here too

second illustration:

result = []
 
>>> for k, g in groupby(s, key=lambda ch: ch.isdigit()):
        if k: result.append(int(''.join(list(g))))  # convert the joined digits to integer
>>> print(result)
[432, 331]

CodePudding user response:

Why not regex though? here is one way

string="".join([x if (x.isdigit()) else " " for x in list(string)]
              ).strip().replace(' ',',',1
                       ).replace(' ','')
print("Result:", [int(i) for i in string.split(',')])


Result: [432, 331]

CodePudding user response:

One way to do this would be to create an empty list and a temporary string variable, then to loop through the letters in the string. For each iteration, you check if the current character is a digit. If it is, append it to to the temporary string. If it isn't, check if the temp string is empty. If it's empty, do nothing. If there's a value in the temp string (say, '1345'), add the temp string value to the list and reset the value of the temp string back to ''. You'll need to make sure you handle the edge case where there's a number in the temporary string when the loop ends also so you don't have an incomplete list.

Here's some pseudocode:

list = []
temp = ''

for char in string {
    if char is digit {
        append char to temp
    }
    else {
        if temp != '' {
            append temp to list
            temp = ''
        }
    }
}

if temp != '' {
    append temp to list
}
  • Related