Home >Java >javaTutorial >How to Match Nested Brackets Without Recursion or Balancing Groups?
Matching Nested Brackets Without Recursion or Balancing Groups
Challenge:
Match a set of arbitrarily nested brackets using a regex flavor that lacks recursion and balancing group support, such as Java's java.util.regex, capturing three outer groups within the given string:
(F(i(r(s)t))) ((S)(e)((c)(o))(n)d) (((((((Third)))))))
Solution: Forward References to the Rescue
Contrary to common belief, matching nested brackets without these advanced features is possible using forward references:
(?=\()(?:(?=.*?\((?!.*?)(.*\)(?!.*).*))(?=.*?\)(?!.*?)(.*)).)+?.*?(?=)[^(]*(?=$)
Break it Down:
This complex regex operates in two stages:
How it Works:
Inner Group Matching Variant:
For matching inner groups, the strategy remains the same, but a capturing group is used to save the matched content within a balanced pair of parentheses:
(?=\()(?=((?:(?=.*?\((?!.*?)(.*\)(?!.*).*))(?=.*?\)(?!.*?)(.*)).)+?.*?(?=)[^(]*(?=$)))
Comprehensive Breakage:
A table summarizes the components and functionality of the regex:
Note | Component | Description |
---|---|---|
(?=() | Look for '(' | |
(?: | Start group for iteration | |
(?=.?((?!.?1)) | Look for '(' not followed by 1, which contains the matched inner content | |
(.)(?!.2).*)) | Capture inner content and check for at least one more ')' | |
(?=.?)(?!.?3)) | Look for ')' not followed by 2, which contains the matched outer content | |
(. ) | Capture outer content | |
. | Consume a character | |
) | Close group | |
? | Match as few times as possible | |
.*?(?=1) | Match up to and including the last '(' | |
1*(?=2$) | Match up to the last ')' without encountering more '(' |
The above is the detailed content of How to Match Nested Brackets Without Recursion or Balancing Groups?. For more information, please follow other related articles on the PHP Chinese website!