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