Marco Nätlitz Marco Nätlitz - 4 months ago 8
Java Question

RegExp: Key Value pairs with value lists enclosed by { and } or quotes

I am looking for a regular expression that will capture the following:


  1. key

  2. key=value

  3. key="some \"value\" escaped"

  4. key={value1,value2}



The key/value pair will be joined by a string, so:

key,key1=value1,key2={foo,bar},key3="some description for \"foo\"",...


Currently, I have the following:

(\w+)|((?:\\.|[^=,]+)*)=("(?:\\.|[^"\\]+)*"|(?:\\.|[^,"\\\{\}]+)*)


It captures (1), (2) and (3) but not (4).

The regular expression will be parsed with Java.

Any ideas?

Answer

You can use this longish regex:

(\b\w+)(?:=((?:\{[^}]+\}|"[^"\\]*(?:\\.[^"\\]*)*"|\w+)))?

RegEx Demo

Comments