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 ".+" ?)+
Était-ce utile?

La 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 "[^"]+"/)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top