Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

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..
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

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?
ASKER CERTIFIED SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

@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
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
Avatar of shragi

ASKER

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"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial