Clement Attlee
Clement Attlee

Reputation: 733

regex to capture number in text

I want to extract the number in text with a pattern like this:

About 181,000 results

if I separate these by space, I will get an error when I try to convert the number to integer by int('181,000'). How can I capture this number correctly and convert it to an integer?

Upvotes: 1

Views: 90

Answers (2)

Jan
Jan

Reputation: 43169

Use the often overlooked locale module with atoi():

import re, locale
from locale import atoi

locale.setlocale(locale.LC_ALL, 'en_US')

string = "About 181,000 results"

rx = re.compile(r'\b\d+(?:,\d+)*\b')

numbers = [atoi(m.group(0)) for m in rx.finditer(string)]
print(numbers)

Which yields

[181000]

Upvotes: 2

Joe Iddon
Joe Iddon

Reputation: 20414

Without a regex, you could achieve a O(n) solution:

>>> s = "About 181,000 results"
>>> int(''.join(i for i in s if i.isdigit()))
181000

Upvotes: 1

Related Questions