Domanda

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 ".+" ?)+
È stato utile?

Soluzione

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 "[^"]+"/)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top