Mindaugas
Mindaugas

Reputation: 17

Regex select text inside quotation marks into groups

I have a lot of lines in this format. I need to capture the quoted text blocks (including quotes), but the last block may be null (without quotes).

"1" "Melbourne is the capital of Australia."    "0" "Canberra is the capital of Australia."
"2" "New York is the capital of Florida."   "0" "Tallahassee is the capital of Florida."
"3" "Paris is the capital of France."   "1" null

Expected replacement after match:

{"id":\1,"statement":\2,"correct":\3,"additional":\4},

Upvotes: 1

Views: 48

Answers (1)

Bohemian
Bohemian

Reputation: 425043

Since you know there are exactly 4 terms:

.*?(".*?").*?(".*?").*?(".*?").*?(null|(".*?").*

See demo

It's important to use reluctant quantifiers, which capture as little as possible.

Upvotes: 1

Related Questions