Kris_1313
Kris_1313

Reputation: 97

Java Pattern class - Which regex to make for getting string from outer brackets?

I'm trying to get string from other string having multiple layers of brackets, but I have problem with making good regular expression. How looks desired expression?

I tried expression \((.*?)\) but the expression is getting first bracket which see...

For example, when I have input string

(many things including things in inner brackets (like this))

I expect group(1) having value

many things including things in inner brackets (like this)

but actual value of group(1)

is many things including things in inner brackets (like this

Upvotes: 0

Views: 35

Answers (1)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You need to get rid of that non-greedy quantifier so your regex can match exhaustively and only stop at last ) instead of the earlier one which is happening because of your non-greedy expression. Just change your regex from \((.*?)\) to \((.*)\)

Demo

Upvotes: 2

Related Questions