I have the following string:
one two three four five six seven eight nine
And I am trying to construct a regular expression that groups the string into three groupings:
- Group 1: 'one two three'
- Group 2: 'four five six'
- Group 3: 'seven eight nine'
I have tried variations of (.*\b(one|two|three)?)(.*\b(four|five|six)?)(.*\b(seven|eight|nine)?)
but this pattern splits the full match into one group that contains the full string - the demo can be found here.
Trying (.*\b(one|two|three))(.*\b(four|five|six))(.*\b(seven|eight|nine))
seems to get me closer to what I want but the match information panel shows that the pattern identifies two matches each containing six capture groups.
I am using the OR statement because the groups can be of any length, e.g. two three four
, applying the pattern to this string should identify two groups -
- Group 1: 'two'
- Group 2: 'three four'.