Question: I am currently writing a java program with regular expression but I am struggling as I am 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 is `key(value)+key(value)+key(value)`. Key is alphanumeric and value is allowed to be any combination.

This has been okay so far. However, I have a problem with `'('`, `')'` in value. If I place `'('` or `')'` in the value, value includes all the rest.

e.g. `number(abc(kk)123)+status(open)` returns `key:number`, `value:abc(kk)123)+status(open`  

It is supposed to be two pairs of key-value.

Can you guys suggest to improve the expression above?

Answer: 
Not possible with regular expressions at all, sorry. If you want to count opening and closing parantheses, regular expressions are, in general, not good enough. The language you are trying to parse is not a [regular language](http://en.wikipedia.org/wiki/Regular_language).

Of course, there may be ways around that limitation. We cannot know that if you give us as little context as you did.
> 
> Someone posted an answer with a working solution regex: `([a-zA-z0-9]+)\((.*?)\)(?=\+|$)` - This works great. When I tested on online regex tester site and came back, the post had gone. Is it right solution? I am wondering why the answer has been deleted.
> 
> 
> 

See this golfed regex:

```
([^\W_]+)\((.*?)\)(?![^+])

```

* You can use a shorthanded character class `[^\W_]` instead of `[a-zA-Z0-9]`.
* You can use a negative lookahead assertion `(?![^+])` to match without backtracking.

**However, this is not a practical solution as `)+` within inner elements will break: `number(abc(kk)+5+123+4+4)+status(open)`**

This is the case where Java, which has the regex implementation that doesn't support recursion, is disadvantaged. As I mentioned in [this thread](https://stackoverflow.com/q/24955473/3622940), the practical approach would be to use a workaround (copy-paste regex), or build your own finite state machine to parse it.

Also, you have a typographical error in your original regex. `[a-zA-z0-9]+` has a range "`A-z`". You meant to type "`A-Z`".