Home > Enterprise >  Extract space separated numbers from string
Extract space separated numbers from string

Time:10-25

So i wanna extract number of total rates from a string in python. Lets say i have string like this:

str = "National Museum 4.6(1 686), Museum, Green Street 24/26, Some description, Closing: md.. 20:00"

i wanna extract rate which is 4.6 and total rates which is 1686. I have managed to extract the 4.6 by following command:

rate = re.findall("\d{1}[.]\d{1,2}", str)
print(rate)
Out: ['4.6']

But i have problem with total rates because sometimes the value can be three digit like 386 without any space and sometimes it can be 12 893 where we have to account the space between.

CodePudding user response:

First of all, note that str is a built-in in Python, you should avoid naming variables with built-in names.

You can extract the specifically formatted number inside parentheses with

re.findall(r"\((\d{1,3}(?:\s\d{3})*)\)", text)

Or, to just get the first match using re.search:

m = re.search(r"\((\d{1,3}(?:\s\d{3})*)\)", text)
if m:
    print(m.group(1))

See the regex demo. Details:

  • \( - a ( char
  • (\d{1,3}(?:\s\d{3})*) - Group 1:
    • \d{1,3} - one, two or three digits
    • (?:\s\d{3})* - zero or more occurrences of a whitespace and three digits
  • \) - a ) char.
  • Related