具有多个组的正则表达式? [英] RegEx with multiple groups?

查看:40
本文介绍了具有多个组的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在 Python 中返回多个组感到困惑.我的正则表达式是这样的:

I'm getting confused returning multiple groups in Python. My RegEx is this:

lun_q = 'Lun:\s*(\d+\s?)*'

我的字符串是

s = '''Lun:                     0 1 2 3 295 296 297 298'''`

我返回一个匹配的对象,然后想查看组,但它显示的是最后一个数字(258):

I return a matched object, and then want to look at the groups, but all it shows it the last number (258):

r.groups()  
(u'298',)

为什么不返回 0,1,2,3,4 等组?

Why isn't it returning groups of 0,1,2,3,4 etc.?

推荐答案

你的正则表达式只包含一对括号(一个捕获组),所以你的匹配中只能得到一个组.如果您在捕获组(+*)上使用重复运算符,则每次重复该组时该组都会被覆盖",这意味着只有最后一个匹配是捕获.

Your regex only contains a single pair of parentheses (one capturing group), so you only get one group in your match. If you use a repetition operator on a capturing group (+ or *), the group gets "overwritten" each time the group is repeated, meaning that only the last match is captured.

在您的示例中,您最好将 .split() 与正则表达式结合使用:

In your example here, you're probably better off using .split(), in combination with a regex:

lun_q = 'Lun:\s*(\d+(?:\s+\d+)*)'
s = '''Lun: 0 1 2 3 295 296 297 298'''

r = re.search(lun_q, s)

if r:
    luns = r.group(1).split()

    # optionally, also convert luns from strings to integers
    luns = [int(lun) for lun in luns]

这篇关于具有多个组的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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