theReverseFlick
theReverseFlick

Reputation: 6044

Regular expression to form a pattern containing a group words

I want to do something like this in Ruby

I have a text like this

    some_random_text unit 1 some_random_text chap 3 some_random_text

Now I want to extract

    some_random_text, 'unit 1', some_random_text, 'chap 3' 

For this I use an expression like this

    my_string.split(/(unit[1-9 ]+|chap[1-9 ]+)/)

I repeat the pattern [1-9 ]+ for both 'unit' and 'chap' because if I group like

   /((unit|chap)[1-9 ]+)/

It returns

    some_random_text, 'unit', 'unit 1', some_random_text, 'chap', 'chap 3' 

which has extra elements I don't need.

How do I do the grouping I need?

Upvotes: 1

Views: 1290

Answers (1)

Kobi
Kobi

Reputation: 138017

Try this:

my_string.split(/((?:unit|chap)[1-9 ]+)/)

Your split regex contains two capturing groups, (...), which are included in the result. Using (?:...) will not capture the smaller group, and is accordingly named a non-capturing group.

Also, note that [1-9 ]+ may capture multiple spaces and numbers, but never zeros for example unit 1 2 4. You may want /((?:unit|chap) +[1-9])/, or /((?:unit|chap) +[1-9][0-9]*)/

Upvotes: 1

Related Questions