Regex - Saving Repeating Captured Group

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 10:30:53

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"]

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).

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.

Working example: http://ideone.com/nnmki

That's just how capturing groups work. If you want to save all of those substrings, put the quantifier inside the capturing group:

a = a.match(/(^%\w+)((?:[.#]\w+)+)/)

Then your second capture will be:

2:".rockets#diamonds.ribbons.forever"

...and you can break it down the rest of the way yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!