正则表达式-保存重复捕获的组 [英] Regex - Saving Repeating Captured Group

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

问题描述

这就是我正在做的事情

a = "%span.rockets#diamonds.ribbons.forever"
a = a.match(/(^\%\w+)([\.|\#]\w+)+/)
puts a.inspect

这就是我得到的

#<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".forever">

这就是我想要的

#<MatchData "%span.rockets#diamonds.ribbons.forever" 1:"%span" 2:".rockets" 3:".#diamonds" 4:".ribbons" 5:".forever">

帮助?我尝试并失败了:(

help? I tried and failed :(

推荐答案

通常,您无法获得任意数量的捕获组,但是如果使用扫描,您可以获得每个要捕获的令牌的匹配

Generally, you can't get an arbitrary number of capturing groups, but if you use scan you can get a match for every token you want to capture:

a = "%span.rockets#diamonds.ribbons.forever"
a = a.scan(/^%\w+|\G[.|#]\w+/)
puts a.inspect

["%span", ".rockets", "#diamonds", ".ribbons", ".forever"]

这与您的正则表达式没有太大区别,但是我删除了最后一个标记上的重复。 \G 不太为人所知-它告诉引擎匹配上一个匹配项的结束位置,因此当您在匹配项之间有多余的字符时,它不会中断(%span:P。火箭)。

This isn't too different from your regex, but I removed repetition on the last token. \G isn't too well known - it tells the engine to match where the previous match ended, so it doesn't break when you have extra characters between matches (%span :P .rockets).

通常,如果您的原始正则表达式有多个匹配项,则此方法可能会增加一些工作,因为您没有分组以匹配,但由于 match 返回单个结果,因此应该可以正常工作。

Generally, if you had multiple matches of your original regex this method may add some work, because you don't have the groups separated to matches, but since match returns a single result it should work fine.

工作示例: http://ideone.com/nnmki

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

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