Question

I need to extract the \d values before the characters enclosed in quotes, so the result for

teststring.scan(/regex/)

should be

=> [["4"], ["2"], ["1"]]

Teststring:

testtring = '4 x "UM 4 ENGLISH" 2 x "UM FRENCH" 1 x "SOME OTHER STRING WHICH COULD CONTAIN 2 x"'

My first approach was something like this, in different variations:

(\d) x ".+" ?
((\d) x ".+" ?)+
Was it helpful?

Solution

Exclude qoutes inside the quoted parts:

teststring.scan(/(\d) x "[^"]+"/)

Alternatively, use non-greedy match:

teststring.scan(/(\d) x ".+?"/)

Also, consider using a quantifier for the digits to support more than one digit.

teststring.scan(/(\d+) x "[^"]+"/)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top