来自一个子组的多个结果(Multiple results from one subgroup)

编程入门 行业动态 更新时间:2024-10-22 15:36:36
来自一个子组的多个结果(Multiple results from one subgroup)

我有这个字符串: <own:egna attribute1="1" attribute2="2">test</own:egna> 我想用正则表达式捕获所有属性。

此正则表达式匹配一个属性: (\s+attribute\d=['"][^'"]+['"]) 但为什么附加一个像``(\ s + attribute \ d = ['“] [^'”] + ['“])+`实际上只返回最后一个匹配的属性,而不是全部?

您如何更改此选项以返回单独组中的所有属性? 我实际上有更多的regexp,因此使用python的findall和等价物等功能是不行的。

I have this string: <own:egna attribute1="1" attribute2="2">test</own:egna> I want to catch all attributes with a regexp.

This regexp matches one attribute: (\s+attribute\d=['"][^'"]+['"]) But why is it that appending a + like ``(\s+attribute\d=['"][^'"]+['"])+` actually only returns the last matched attribute and not all of them?

How would you change this to return all attributes in separate groups? I'm actually having more regexp around this, so using functions such as python's findall and equivalents won't do.

最满意答案

简短的回答是你不能 - 只有最后一组可以访问。 Python文档明确说明了这一点:

如果一个组匹配多次,则只能访问最后一个匹配[...]

你必须使用一些语言功能:

在PHP中,有preg_match_all返回所有匹配项。 在其他语言中,您必须手动执行此操作:将g修饰符添加到正则表达式并在其上循环。 例如,Perl将管理一个字符串位置,并在每次匹配/([...])/g模式时以$ 1返回下一个匹配。

另请参阅捕获重复组 。

The short answer is you can't - only the last group is accessible. The Python docs state this explicitly:

If a group matches multiple times, only the last match is accessible [...]

You'll have to use some language features:

In PHP, there's preg_match_all that returns all matches. In other languages, you'll have to do this manually: add the g modifier to the regex and loop over it. Perl, for example, will manage a string position and return the next match in $1 each time a /([...])/g pattern is matched.

Also take a look at Capturing a repeated group.

更多推荐

本文发布于:2023-08-04 09:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   个子   Multiple   results   subgroup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!