Answer the question
In order to leave comments, you need to log in
How to make a regular expression that determines the level of nesting of loops?
For a program containing several nested loops, use regular expressions to determine the nesting level of loops.
those. how it works:
We feed the regular expression the code:
...
for(var i=0;i<10;i++){
something();
if(){
....
}
while(true){
for(var i=0;i!=3;i++){
...
}
}
}
...
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
i++;
}
(for|while){(*^})(if{*})(*^})(for|while)
Answer the question
In order to leave comments, you need to log in
Regular expression is not designed for computation and is not well suited for recursive nesting. IMHO, a lexical analyzer is needed here.
As an option, combine the regular expression and its consistent application:
To obtain an "embracing" cycle (its body)
, then pull out the contents of the match, and apply the same pattern to it, depth +1, and so on. while the content is matched by regular match
Threat regular season from C#, I didn’t check it in Java, but I should be able to handle it
Solutions (by task type, in ascending order of CPU load):
1. traversal of the "tree"
2. reverse Polish notation
3. regular expressions, reverse recursive lookup for a given match
Choose.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question