Using regular expressions, this (bracketing) can , but only to a fixed level of nesting.
Your current regex (slightly changed):
(@Override[\n\s\t]*)?public *(abstract)? *void *[az]*\([az]* [^)]+\)[\n\\s\t]*((\{[^\{\}]*?\})|;)
Only one level. More specifically, this is the part that corresponds to it:
(\{[^\{\}]*?\})
If you want to combine up to two levels, change the specified part to:
(\{([^{}]*|\{[^{}]*\})*\})
In this way:
(@Override[\n\s\t]*)?public *(abstract)? *void *[az]*\([az]* [^)]+\)[\n\\s\t]*(\{([^{}]*|\{[^{}]*\})*\}|;)
To add additional levels, you must continue to edit. It will become more messy and messy when you add levels.
Explanation:
\{ will match the first open bracket( opens a group[^{}]* matches anything other than brackets| or\{ if he finds an opening bracket ...[^{}]* ... it will match anything but the parenthesis ...
\} ... until it finds a closing bracket
) closes the group* above group may consist of zero or more times
\} matches end bracket
To add other levels, change the middle (second) part of [^{}]* to ([^{}]*|\{[^{}]*\})* .
If you cannot predict the maximum level of nesting:
There are several languages ββthat allow the nesting operator R , which allows you to set an arbitrary number of levels. If your language does not support it (Java does not work, PHP and Perl afik), you will have to either:
- predict the maximum level of nesting; OR
- create a parser yourself.
Regular expressions without the R operator cannot set any number of levels.
However, using the R operator will be like this:
(\{([^{}]|(?R))*\})
More info on this answer .