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\,]+)
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)
([^=\s,]+)=(?|"([^"]*)"|([^\s,]+))
- Wiktor Stribiżew