python正则表达式,只返回第一个数字匹配

时间:2021-06-11 03:32:27

标签: python regex

我正在使用 Python 正则表达式。我有一种情况,为了运行我的正则表达式,文本并不总是返回相同的文本。文本可以有 4 个不同的选项。我只需要返回数值,包括任何小数

如果它是第一个 2 个选项,则直接进行。如果是第 3/4 个选项,则两者都匹配。我只需要返回第一次出现,无论它是否有小数,只是第一次。我有这个正则表达式,但它匹配两次出现,我只需要第一次出现。

我可以在这个正则表达式中添加什么以返回第一次出现(或者对于我的问题更好的正则表达式)

我的正则表达式是 [approx. |Current ] distance(\d+\.?\d+) miles

option1
Curent distance4124 miles
>> regex output = 4124

option2
approx. distance4124 miles
>> regex output = 4124

option3
approx. distance4124.2 miles
Current distance4124 miles
>> regex output = 4124.2

options4
Current distance4124 miles
approx. distance4124.2 miles
>> regex output = 4124

1 个答案:

答案 0 :(得分:2)

解决方案

python 中有一个 re.finditer(pattern, string, flags=0)

Return an iterator yielding match objects over all non-overlapping 
matches for the RE pattern in string. The string is scanned left-to- 
right, and matches are returned in the order found. Empty matches are 
included in the result.

可以使用next(match_object)获取第一个匹配对象。


错误

请注意您的正则表达式不正确。

  • 正如@tripleee 在评论中提到的那样,你必须使用 (...|...) 代替 [...]

  • 匹配十进制数的部分也不正确。它应该是 \d+(\.\d+)?。您的正则表达式与字符串 approx. distance4 miles 不匹配。

  • 您还在正则表达式中使用了额外的空间,导致匹配失败


代码

使用的正则表达式: \d+(?:\.\d+)?

findall 中使用此正则表达式将返回在输入文本中找到的所有数字,因此我使用了 finditer。您也可以使用 search

import re

line = input()
match = re.finditer("\d+(?:\.\d+)?", line)
print()
print(next(match).group(0))

示例 IO

Input 1:
approx. distance4124.2 miles
Current distance4124 miles

Output 1:
4124.2

Input 2:
Current distance4124 miles

Output 2:
4124

如果您遇到任何其他问题,请发表评论。