在一行中获取捕获的组 [英] Getting captured group in one line

查看:42
本文介绍了在一行中获取捕获的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有匹配,有一个已知的模式"来获取捕获的组值或空字符串:

match = re.search('regex', 'text')如果匹配:值 = match.group(1)别的:值 = ""

或:

match = re.search('regex', 'text')value = match.group(1) 如果匹配 else ''

有没有一种简单的 Pythonic 方法可以在一行中做到这一点?

换句话说,如果找不到捕获组,我可以为其提供默认值吗?

<小时>

例如,我需要从 key= 字符串之后的文本中提取所有字母数字字符(和 _):

<预><代码>>>>进口重新>>>PATTERN = re.compile('key=(\w+)')>>>def find_text(text):... match = PATTERN.search(text)...返回 match.group(1) 如果匹配 else ''...>>>find_text('foo=bar,key=value,beer=pub')'价值'>>>find_text('这里没有匹配')''

find_text() 是否有可能是单行的?

这只是一个例子,我正在寻找一种通用的方法.

解决方案

引用自 MatchObjects 文档

<块引用>

匹配对象总是有一个布尔值 True.由于 match()search() 在没有匹配的时候返回 None,你可以用一个简单的 if 语句测试是否有匹配:

match = re.search(pattern, string)如果匹配:过程(匹配)

由于没有其他选择,并且当您使用一个函数时,我想提出这个替代方案

def find_text(text,matches = lambda x: x.group(1) if x else ''):返回匹配项(PATTERN.search(文本))断言 find_text('foo=bar,key=value,beer=pub') == 'value'assert find_text('这里没有匹配') == ''

完全一样,但只有你需要做的检查被默认参数化了.

在评论中想到@Kevin的解决方案和@devnull的建议,你可以这样做

def find_text(text):return next((item.group(1) for item in PATTERN.finditer(text)), "")

这利用了这样一个事实,next 接受作为参数返回的默认值.但这有在每次迭代时创建生成器表达式的开销.所以,我会坚持第一个版本.

There is a known "pattern" to get the captured group value or an empty string if no match:

match = re.search('regex', 'text')
if match:
    value = match.group(1)
else:
    value = ""

or:

match = re.search('regex', 'text')
value = match.group(1) if match else ''

Is there a simple and pythonic way to do this in one line?

In other words, can I provide a default for a capturing group in case it's not found?


For example, I need to extract all alphanumeric characters (and _) from the text after the key= string:

>>> import re
>>> PATTERN = re.compile('key=(\w+)')
>>> def find_text(text):
...     match = PATTERN.search(text)
...     return match.group(1) if match else ''
... 
>>> find_text('foo=bar,key=value,beer=pub')
'value'
>>> find_text('no match here')
''

Is it possible for find_text() to be a one-liner?

It is just an example, I'm looking for a generic approach.

解决方案

Quoting from the MatchObjects docs,

Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement:

match = re.search(pattern, string)
if match:
   process(match)

Since there is no other option, and as you use a function, I would like to present this alternative

def find_text(text, matches = lambda x: x.group(1) if x else ''):
    return matches(PATTERN.search(text))

assert find_text('foo=bar,key=value,beer=pub') == 'value'
assert find_text('no match here') == ''

It is the same exact thing, but only the check which you need to do has been default parameterized.

Thinking of @Kevin's solution and @devnull's suggestions in the comments, you can do something like this

def find_text(text):
    return next((item.group(1) for item in PATTERN.finditer(text)), "")

This takes advantage of the fact that, next accepts the default to be returned as an argument. But this has the overhead of creating a generator expression on every iteration. So, I would stick to the first version.

这篇关于在一行中获取捕获的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆