I think you want to match pairs of brackets at any level, right? Simply speaking, it is not feasible to use regex.
A regular expression is essentially a finite state machine. A finite state machine does not store anything other than the current state it is in. In terms of matching pairs of brackets at any level, storage of arbitrary length (corresponding to the level) is required. Hard-coded regular expressions cannot do this, and libraries that solve this problem also require dynamic memory. See: http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns
If your problem is more general, such as any pair of labels. The most common ones are XML or HTML. Due to the diversity and compatibility of syntax, this will be more difficult in practice. You basically have to use a library. See: http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg
The regular engine in the standard library is not supported. This regex library supports it. You can find the usage by searching for "recursive" on the web page.
I think you want to match pairs of brackets at any level, right? Simply speaking, it is not feasible to use regex.
A regular expression is essentially a finite state machine. A finite state machine does not store anything other than the current state it is in.
In terms of matching pairs of brackets at any level, storage of arbitrary length (corresponding to the level) is required. Hard-coded regular expressions cannot do this, and libraries that solve this problem also require dynamic memory. See: http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns
If your problem is more general, such as any pair of labels. The most common ones are XML or HTML. Due to the diversity and compatibility of syntax, this will be more difficult in practice. You basically have to use a library. See: http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg
The regular engine in the standard library is not supported. This regex library supports it. You can find the usage by searching for "recursive" on the web page.
The correct solution on the first floor can be achieved using a stack.