Search code examples
ruby-on-railsregexrubular

Rails Regex Match Group Overwriting itself


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:

  1. NFPA
  2. 101
  3. 19
  4. 7
  5. 2
  6. 2

What I get is this:

  1. NFPA
  2. 101
  3. 2

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) (.+) (.+?.)+(.+)


Solution

  • 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