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)

enter image description here

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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -