I need a regular expression to select all the text between two outer brackets.
Example: some text(text here(possible text)text(possible text(more text)))end text
Result: (text here(possible text)text(possible text(more text)))
I need a regular expression to select all the text between two outer brackets.
Example: some text(text here(possible text)text(possible text(more text)))end text
Result: (text here(possible text)text(possible text(more text)))
I want to add this answer for quickreference. Feel free to update.
.NET Regex using balancing groups.
Where
c
is used as the depth counter.Demo at Regexstorm.com
PCRE using a recursive pattern.
Demo at regex101; Or without alternation:
Demo at regex101; Or unrolled for performance:
Demo at regex101; The pattern is pasted at
(?R)
which represents(?0)
.Perl, PHP, Notepad++, R: perl=TRUE, Python: Regex package with
(?V1)
for Perl behaviour.Ruby using subexpression calls.
With Ruby 2.0
\g<0>
can be used to call full pattern.Demo at Rubular; Ruby 1.9 only supports capturing group recursion:
Demo at Rubular (atomic grouping since Ruby 1.9.3)
JavaScript API :: XRegExp.matchRecursive
JS, Java and other regex flavors without recursion up to 2 levels of nesting:
Demo at regex101. Deeper nesting needs to be added to pattern.
To fail faster on unbalanced parenthesis drop the
+
quantifier.Java: An interesting idea using forward references by @jaytea.
Reference - What does this regex mean?
It is actually possible to do it using .NET regular expressions, but it is not trivial, so read carefully.
You can read a nice article here. You also may need to read up on .NET regular expressions. You can start reading here.
Angle brackets
<>
were used because they do not require escaping.The regular expression looks like this:
I have written a little JavaScript library called balanced to help with this task. You can accomplish this by doing
You can even do replacements:
Here's a more complex and interactive example JSFiddle.
This is the definitive regex:
Example:
note that the
'(pip'
is correctly managed as string. (tried in regulator: http://sourceforge.net/projects/regulator/)This one also worked