java - regular expression for key=(value) syntax -
i writing java program regular expression struggling pretty new in regex.
key_expression = "[a-za-z0-9]+"; value_expression = "[a-za-z0-9\\*\\+,%_\\-!@#\\$\\^=<>\\.\\?';:\\|~`&\\{\\}\\[\\]/ ]*"; chunk_expression = "(" + key_expression + ")\\((" + value_expression + ")\\)";
the target syntax key(value)+key(value)+key(value)
. key alphanumeric , value allowed combination.
this has been okay far. however, have problem '('
, ')'
in value. if place '('
or ')'
in value, value includes rest.
e.g. number(abc(kk)123)+status(open)
returns key:number
, value:abc(kk)123)+status(open
supposed 2 pairs of key-value.
can guys suggest improve expression above?
get matched group index 1 , 2
([a-za-z0-9]+)\((.*?)\)(?=\+|$)
here online demo
the above regex pattern looks of )+
delimiter between keys , values.
note: above regex pattern not work if value contains )+
example number(abc(kk)+123+4+4)+status(open)
sample code:
string str = "number(abc(kk)123)+status(open)"; pattern p = pattern.compile("([a-za-z0-9]+)\\((.*?)\\)(?=\\+|$)"); matcher m = p.matcher(str); while (m.find()) { system.out.println(m.group(1) + ":" + m.group(2)); }
output:
number:abc(kk)123 status:open
Comments
Post a Comment