0
votes

I need to parse a key-value string. The regex should get the text after whitespace (red line) in the second capture group (value).

Currently used regex:

([^=\s,]+)=([^\s\,]+)

enter image description here

1
Try ([^=\s,]+)=(?|"([^"]*)"|([^\s,]+)) - Wiktor Stribiżew

1 Answers

1
votes

One approach to deal with the edge case of the key being in quotation marks would be to use an alternation:

\b([^=]+)=(?:"(.*?)"|([^\s,]+))

This matches a key, followed by =, then following by either:

"(.*?)"    a term in double quotes (capture only what is inside quotes)
|          OR
[^\s,]+    any continuous block of non whitespace characters (non quote case)

Demo