Match everything inside brackets (nested brackets)

I have a regex that matches everything inside the brackets:

 ?\(.*?\)

I need to adjust this regex, so it also matches nested brackets, for example:

ABC ( DEF (GHI) JKL ) MNO

Must match (DEF (GHI) JKL) in this example

+4
source share
2 answers

To match ( DEF (GHI) JKL )in ABC ( DEF (GHI) JKL ) MNO, you must change .*?to .*regex in your example:

\(.*\)

.*? - lazy - it will correspond to the shortest possible line;

.* is greedy - it will match the longest possible string.

+2
source

If you want to combine:

ABC (DEF (GHI) JKL) MNO

:

?\(.*\)

: https://regex101.com/r/5Y5ZM0/2

EDIT: @GameDroids

+1

Source: https://habr.com/ru/post/1692372/


All Articles