문제

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 ".+" ?)+
도움이 되었습니까?

해결책

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 "[^"]+"/)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top