Reputation: 167
I've got the following text in a text file and want to extract (1622,2096), (1755,1976) and the digits after the colon (ie 3.051753206) all from the second line.
Distances (mm):
Distance from (1622,2096) to (1755,1976) with intensity 255: 3.051753206
Eye diameter (mm): 24
Here's the code I have currently, which I think should extract the digits at the end of the line:
match = re.search(r'Distance from \((\d+)),(\d+)\) to \((\d+)),(\d+)\) with intensity 255: (\d+)', line)
if match:
d_mm = int(match.group(1))
print(d_mm)
Can someone help me adjust my regex expression?
Upvotes: 1
Views: 64
Reputation: 163217
If you want to extract (1622,2096)
and (1755,1976)
then the capturing group should be around that whole part, and matching the parentesis should be also inside the group.
Matching the digits after the could be done adding an extra part to match the decimals.
\bDistance from (\(\d+,\d+\)) to (\(\d+,\d+\)) with intensity 255: (\d+(?:\.\d+)?)
import re
line = ("Distances (mm):\n"
" Distance from (1622,2096) to (1755,1976) with intensity 255: 3.051753206\n"
"Eye diameter (mm): 24")
match = re.search(r'\bDistance from (\(\d+,\d+\)) to (\(\d+,\d+\)) with intensity 255: (\d+(?:\.\d+)?)', line)
if match:
print(match.group(1))
print(match.group(2))
print(match.group(3))
Output
(1622,2096)
(1755,1976)
3.051753206
Upvotes: 1
Reputation: 12493
Here's a slight change to the regex:
m = re.search(r'Distance from \((\d+),(\d+)\) to \((\d+),(\d+)\) with intensity 255: ([\d\.]+)', line)
m.groups()
The output is:
('1622', '2096', '1755', '1976', '3.051753206')
Upvotes: 0