Reputation: 7560
I'm trying to exclude matches within quotes in RegEx. This String is my subject:
ONEKEY=VAL1, TWOKEY=VAL2, THREEKEY="VAL3.1, VAL3.2", FOURKEY=VAL4
I want to split this string (using NSRegularExpression
in Mac OS) and get an associative array. To make it easier I first wanted to split the string into KEY=VALUE
pairs and either easily explode them by =
or use another RegEx in my iteration. My problem now is that I can't get exclusion of the quoted values work. Here is my RegEx:
(?=(.))([^,\s]*)
I already tried something like this: (?=(.))([^"])?([^,\s]*)([^"])?
and (?=([^"]?.[^"]?))([^,\s]*)
Upvotes: 1
Views: 2555
Reputation: 55609
How about finding:
([^=]+)=("[^"]*"|[^,]+)(?:,\s*)?
Then extract group 1 (the key) and 2 (the value(s)).
[^=]+
- one or more characters that aren't =
.
"[^"]*"
- any characters between quotes.
[^,]+
- one or more characters that aren't ,
.
(?:,\s*)?
- consume the ,
with any spaces after it (the only difference between (...)
and (?:...)
is that the latter doesn't assign a group to it).
This won't work if brackets can be nested.
Test.
Upvotes: 2