简体   繁体   English

您如何将这些组与 Python 正则表达式匹配?

[英]How do you match these groups with Python regex?

I have a weird case where this simple code is not functioning as expected:我有一个奇怪的情况,这个简单的代码没有按预期运行:

import re

text = 'This Level: 75.3'
matches = re.search(r'(?:(?:\d{1,3},)(?:\d{3},)*(?:\d{3})|\d*)(?:\.\d+)?', text)

print(matches.match)

I keep getting a blank string returned... however, I would expect this to be 75.3 .我不断返回一个空白字符串......但是,我希望这是75.3 This works for other use cases, such as:这适用于其他用例,例如:

assert util.strip_str_to_float('7') == 7.0
assert util.strip_str_to_float('75') == 75.0
assert util.strip_str_to_float('75.5') == 75.5
assert util.strip_str_to_float('7.7.9') == 7.7
assert util.strip_str_to_float('1,298.3 Gold') == 1298.3

Ultimately, I'm trying to pull out and convert the first float from a given string... I wasn't expecting this test case to be a failure.最终,我试图从给定的字符串中提取并转换第一个浮点数......我没想到这个测试用例会失败。 It seems to be failing specifically when the matching does not start at the beginning of the string.当匹配不是从字符串的开头开始时,它似乎特别失败。 The search seems to work fine if I remove the non-capturing groups, for example, this works:如果我删除非捕获组,搜索似乎工作正常,例如,这有效:

matches = re.search(r'\d*\.\d+', text)

But this does not:但这不会:

matches = re.search(r'\d*(?:\.\d+)?', text)

Any ideas...?有任何想法吗...?

It looks like you're allowing plain integers without the decimal part as well as decimals like ".5" without the whole number part.看起来您允许没有小数部分的纯整数以及没有整数部分的小数(如“.5”)。 That's great, but since both parts are optional, you're also matching when neither part is present, so you're getting a lot of empty 0-length matches.这很好,但是由于这两个部分都是可选的,所以当两个部分都不存在时,您也会进行匹配,因此您会得到很多空的 0 长度匹配项。

This is also why your pattern r'\d*\.\d+' worked, because the decimal was required.这也是您的模式r'\d*\.\d+'起作用的原因,因为需要小数点。

pattern = r'\d{1,3}(?:,\d{3})*(?:\.\d+)?|\.\d+'

If I'm understanding the question right, this pattern should work.如果我正确理解了这个问题,那么这种模式应该可以工作。 It's divided into two parts, so it looks for either:它分为两部分,因此它查找以下任一部分:

  • a whole number with a decimal part optional, or带有可选小数部分的整数,或
  • a required decimal part, with no whole number before it必需的小数部分,前面没有整数

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

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