I am trying to match this string:
NFPA 101 19.7.2.2
and am using this regex:
(NFPA) (\w+)(?: ?(?:([^.]+)\.?)+)?
This seems to match the string, but the captured groups are not what I'm looking for. I expect:
What I get is this:
See this rubular example: http://rubular.com/r/43VY0yyNa7
It's as if that last recurring capture group is being overwritten by the final match. Is there a way to have all of these come back as capture groups as I need?
Added another regex that gives me the similar problem described above: (NFPA) (.+) (.+?.)+(.+)
The issue is you're using non-capturing group symbol :
which isn't gonna work to select the string as separate capture group. To overcome the issue you need to use Positive / Negative Lookahead. So, the following regex should work in this case :
(\w+|\d+[-]\d+)(?=\s?)(?![-])
see demo