Ketan Modi
Ketan Modi

Reputation: 1800

regex expression for extract amounts

I have a "7 9/16 x 9 1/2 in. (19.21 x 24.13 cm) (image)" amounts, i want to extract cm amount as a height and width

expected output should be 19.21 and 24.13

i have tried re.findall('\(.*?cm\)',value), but this will return list and again i have to split it with x and remove cm from string.

Upvotes: 1

Views: 149

Answers (4)

The fourth bird
The fourth bird

Reputation: 163477

It seems that you intended to match the value between the parenthesis. You might also make the match for the amounts a bit more specific matching 1 or more digits with an optional decimal part \d+(?:\.\d+)?

\((\d+(?:\.\d+)?) x (\d+(?:\.\d+)?) cm\)

Explanation

  • \( Match (
  • (\d+(?:\.\d+)?) Capture group 1, match 1+ digits with an optional decimal part
  • x Match literally
  • (\d+(?:\.\d+)?) Capture group 2, match 1+ digits with an optional decimal part
  • cm\) Match a space and cm)

Example code

import re

value = "7 9/16 x 9 1/2 in. (19.21 x 24.13 cm) (image)"
pattern = r"\((\d+(?:\.\d+)?) x (\d+(?:\.\d+)?) cm\)"

for x in re.findall(pattern, value):
    print (f"{x[0]} and {x[1]}")

Output

19.21 and 24.13

See a Python demo

Upvotes: 2

Tuan Bao
Tuan Bao

Reputation: 266

Hope the regex helps you: \(([\d.]+)(?:[^\d.]+)([\d.]+).+cm\)

In python source:

import re

txt = "7 9/16 x 9 1/2 in. (19.21     x 24.13 cm) (image)"
x = re.findall("\(([\d.]+)(?:[^\d.]+)([\d.]+).+cm\)", txt)
print(x)
for item in x:
    print(item[0] + ' and ' + item[1])


OUTPUT

19.21 and 24.13

Upvotes: 1

Leonardo Scotti
Leonardo Scotti

Reputation: 1080

Try this:

x = "7 9/16 x 9 1/2 in. (19.21 x 24.13 cm) (image)"

import re
y = re.find_all((?:\()(.*)(?:x)(.*)(?:cm\)), x)

Output:

['19.21', '24.13']

Upvotes: 1

Alireza
Alireza

Reputation: 2123

Try this:

([\d.]+)\s*x\s*([\d.]+)(?= cm)

See Regex Demo

Python3 Code:

import re

value = "7 9/16 x 9 1/2 in. (19.21 x 24.13 cm) (image)"

size = list(re.findall('([\d.]+)\s*x\s*([\d.]+)(?= cm)', value)[0])
print(size[0] + " and " + size[1])

Output:

19.21 and 24.13

Upvotes: 1

Related Questions