我有这个字符串: <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.
更多推荐
发布评论