• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

regex to check nested loop

i am looking for a simple solution which checks whether there are any nested loops...

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
For Each p In Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
   For Each R In Range
        R.Foil_Number_From_Scan =R.Tree.KeyEntry_0
   Next R
Next  p
End Sub

the above has nested For loop ...

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
If  p.ColorContent0 = 0 Then
        p.InputImage = p.OutputImage
   Else If
        'If color_content value is not equal to 0 sends to COLOR_CLEANUP
    Else    
     p.IE_COLOR_CLEANUP.InputImage = p.IE_COLOR_DETECT.OutputImage
End If
End Sub


the above has nested If loop ...

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
If  p.ColorContent0 = 0 Then
        p.InputImage = p.OutputImage
  For Each R In Range
        R.Foil_Number_From_Scan =R.Tree.KeyEntry_0
   Next R
End If
End Sub

The above has nested loop but for inside if

so, I want a code to check whether there is any nested loop... if so what is the loop name..
0
shragi
Asked:
shragi
  • 3
  • 2
2 Solutions
 
Terry WoodsIT GuruCommented:
Strictly, you need a code parser for this if you want a 100% accurate result, as cases like this (and more complex cases, such as commented out code) could confuse anything simpler:

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
For Each p In Pages
        p.IE_COLOR_DETECT.InputImage = "For Each R In Range    R.Foil_Number_From_Scan =R.Tree.KeyEntry_0   Next R"
Next  p
End Sub

However, if you're happy with a mostly accurate result it should be possible with regular expressions like this one:
http://www.myregextester.com/?r=66cdd2b4

In the linked example, I'm only looking at a for loop nested in a for loop. If's aren't loops by the way.

Is it only for's and if's that you're interested in?
0
 
iHadiCommented:
Hi

Try the following code. It should match all your examples and return the type of loop (or code block 'If'):

private string CheckLoop(string codeString)
{
    string pattern= @"(?:For Each|If).+?(?:(?<type>For Each).+?Next\w*|(?<type>Else If)).+?(?:Next \w*|End If)";
    Match m = Regex.Match(codeString, pattern,RegexOptions.Singleline|RegexOptions.IgnoreCase);

    if (m.Success)
        return m.Groups["type"].Value;
    else
        return "";
}
0
 
shragiAuthor Commented:
@iHadi...

that Regex worked..but can we extend it to while and do while..

1) while
      while()
      wend
Wend

nested while loop

2) do
        do
           while
     while

nested do while loop

3) while
          do
             while
      wend

dowhile inside while

4) do
           while()
          wend
        while

while inside dowhile


the statment
return m.Groups["type"].Value;

returns End If or for....
there is a problem with that...return...
but there will be no clarity with that...

if the return value is "end if" does it mean if inside for or if inside if
so can we return the two values may be "End if" and "for" if there is for loop inside if
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
iHadiCommented:
Replace the previous regex with the following:

(?:For Each|If|While|Do).+?(?:(?<type>For Each).+?Next\w*|(?<type>Else If)|(?<type>While).+?Wend|(?<type>Do).+?While).+?(?:Next \w*|End If|While|Wend)

And the previous and current code returns Else If and not End If
0
 
shragiAuthor Commented:
hmm ..awesome.....

final one...

i would like to check....one more condition...


Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
If  p.ColorContent0 = 0 Then
        p.InputImage = p.OutputImage
   Else If
        'If color_content value is not equal to 0 sends to COLOR_CLEANUP
    Else    
     p.IE_COLOR_CLEANUP.InputImage = p.IE_COLOR_DETECT.OutputImage
End Sub


the above code missing "end if"
so how do you write regular expression for checking "end if" and "Wend"
0
 
iHadiCommented:
(?:For Each|If|While|Do).+?(?:(?<type>For Each).+?Next\w*|(?<type>Else If)|(?<type>While).+?Wend|(?<type>Do).+?While).+?(?:Next \w*|End If|While|Wend|End Sub)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now