Reputation: 1273
I have this string:
(3 + 5) * (1 + 1)
I want to match this strings:
(3 + 5)
and
(1 + 1)
I used this to try to match it:
(\(.*\))
But that matches the whole string, from the first ( to the last ).
Any idea on how to fix this / make it working?
Upvotes: 1
Views: 378
Reputation: 383726
There are two ways to look at this:
*
, I want reluctant *?
(\(.*?\))
.
, I want [^)]
, i.e. anything but )
(\([^)]*\))
Note that neither handles nested parentheses well. Most regex engine would have a hard time handling arbitrarily nested parantheses, but in .NET you can use balancing groups definition.
Upvotes: 4
Reputation: 523224
If you don't care about nested parenthesis, use
(\([^()]*\))
# ^^^^^
to avoid matching any (
or )
inside a group.
Upvotes: 0