Python 正则表达式检查已找到哪些组
我有以下代码:Python 正则表达式检查已找到哪些组,python,Python,我有以下代码: regex_pattern = r'^(?P<value>[\.\d]+)(\s+)(?P<unit>[a-zA-z/%]+)$' for row in data: name, value = re.search(regex_pattern, str(row[1])) if value is not None and unit is not None:
regex_pattern = r'^(?P<value>[\.\d]+)(\s+)(?P<unit>[a-zA-z/%]+)$'
for row in data:
name, value = re.search(regex_pattern, str(row[1]))
if value is not None and unit is not None:
print(value, unit)
我猜是因为它找不到任何匹配项,所以我可以检查结果是否为无
但是如果找到的是
名称,而不是值,或者反之亦然,我如何检查呢?如果返回无:
name,value=re.search(regex_模式,str(行[1]))
您无法将其解压缩为名称和值。将代码重新编写为:
search=re.search(regex_模式,str(行[1]))
如果搜索不是无:
名称,值=搜索
第三,我不知道\uuuuuu\uuuu
在这里做什么。你想写你的名字吗
或者,您可以将regex设置为可选,以返回名称或值或两者。当前,它必须同时返回这两个组。您的捕获组都不是可选的或有条件的。。。因此,两者都必须存在,否则将无法匹配一旦您解决了要匹配或不想匹配的问题并获得成功匹配,您可能还需要在匹配对象.groupdict()
上执行此操作,然后如果您想进行一些检查,它将为您提供组的名称->组的值look。。。
TypeError: cannot unpack non-iterable NoneType object