Home > Mobile >  Having problems with regex python
Having problems with regex python

Time:09-26

I'm trying to get the numbers in my output.

Below is a sample of my input:

(20,414 results)
(3,004 results)
(91,718 results)
(118 results)
(No result)
(128,234 results)

I just want the numbers. I've tried str.extract('([^\D])') but it only outputs the first number. I've tried various combinations and I'm not able to print out all the numbers.

Any suggestions?

Thank you for taking the time to read my problem and help in any way.

CodePudding user response:

You need findall with [\d|,] :

>>> re.findall('[\d|,] ', s)
['20,414', '3,004', '91,718', '118', '128,234']
>>> 

That matches all digits and commas.

If you want to convert them into integers:

>>> [int(x.replace(',', '')) for x in re.findall('[\d|,] ', s)]
[20414, 3004, 91718, 118, 128234]
>>> 

CodePudding user response:

The actual regex pattern you should be using for whole numbers with possible thousands seprator is:

\d{1,3}(?:,\d{3})*

Sample script:

inp = """(20,414 results)
(3,004 results)
(91,718 results)
(118 results)
(No result)
(128,234 results)"""

output = re.findall(r'\d{1,3}(?:,\d{3})*', inp)
print(output)  # ['20,414', '3,004', '91,718', '118', '128,234']

CodePudding user response:

Something like this might also work:

([0-9] ,?[0-9]*)
  • Related