简体   繁体   English

行尾数字的正则表达式

[英]RegEx for Digits at End of Line

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.我在文本文件中有以下文本,并希望从第二行中提取 (1622,2096)、(1755,1976) 和冒号后的数字(即 3.051753206)。

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?有人可以帮我调整我的正则表达式吗?

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.如果要提取(1622,2096)(1755,1976) ,则捕获组应该围绕整个部分,并且匹配的 parentesis 也应该在组内。

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+)?)

Regex demo |正则表达式演示| Python demo Python 演示

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 Output

(1622,2096)
(1755,1976)
3.051753206

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: output 是:

('1622', '2096', '1755', '1976', '3.051753206')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM