I have a regex that matches everything inside brackets:
?\(.*?\)
I need adjust this regex, so it also matches nested brackets, for example:
ABC ( DEF (GHI) JKL ) MNO
Should match ( DEF (GHI) JKL ) in this example
I have a regex that matches everything inside brackets:
?\(.*?\)
I need adjust this regex, so it also matches nested brackets, for example:
ABC ( DEF (GHI) JKL ) MNO
Should match ( DEF (GHI) JKL ) in this example
To match the ( DEF (GHI) JKL )
in ABC ( DEF (GHI) JKL ) MNO
you should change .*?
to .*
in your example regex:
\(.*\)
.*?
is lazy - it will match shortest possible string;
.*
is greedy - it will match longest possible string.
If you want to match :
ABC ( DEF (GHI) JKL ) MNO
This works:
?\(.*\)
Ref: https://regex101.com/r/5Y5ZM0/2
EDIT: Updated with shorter working version from @GameDroids