Reg Ex loop. I think it's right, but it loops

If anyone has a few minutes to test this out for me I would certainly appreciate. I have used this code many of times, but no matter what I do it keeps looping (The Match never goes nil). It should be going Nil :slight_smile:

Thanks in advance!! :slight_smile:

Here is my (InputPayload) {“IPhostnameURL”:“10.10.10.2”,“Username”:“bob”,“Password”:“pass”,“Enable PW”:“ENpass”,“Auto Select”:“0”,“TELNET”:“1”,“SSH”:“0”,“HTTP”:“0”,“HTTPS”:“0”}

    Dim SeparateJSON_RegEx as RegEx
    Dim SeparateJSON_RegExMatch as RegExMatch
    Dim SeparateJSONRegEx_HitText as String
    SeparateJSON_Regex = New RegEx
    SeparateJSON_Regex.Options.CaseSensitive = true
    SeparateJSON_Regex.Options.Greedy = False
    SeparateJSON_Regex.Options.MatchEmpty = False
    SeparateJSON_Regex.Options.StringBeginIsLineBegin = False
    SeparateJSON_Regex.Options.StringEndIsLineEnd = False
    SeparateJSON_Regex.Options.DotMatchAll = True
    SeparateJSON_Regex.SearchPattern = "[{].+(?<=[}])" 
    
    do
      SeparateJSON_RegexMatch = SeparateJSON_Regex.Search(InputPayload)
      if SeparateJSON_RegexMatch <> nil then
        SeparateJSONRegEx_HitText = SeparateJSON_RegexMatch.SubExpressionString(0)
        TargetInfoInputQueue.Append SeparateJSONRegEx_HitText
        SeparateJSON_RegexMatch = SeparateJSON_Regex.Search
      end if
    loop until SeparateJSON_RegexMatch Is Nil // <-- Never goes Nil 

“Anyone…” Har!

You keep restarting the search from the beginning because you have rx.Search( text ) at the top of your loop, then rx.Search() at the bottom of the If block.

Ah copy and paste Bone Headed move.

I moved " SeparateJSON_RegexMatch = SeparateJSON_Regex.Search(InputPayload)" inside of the Do loop. Not good.

I moved it out and it is fine now :slight_smile:

:wink: Thanks for

CROSS POST: THanks Kem!!!

AND that is why I work for Tips :slight_smile: hehe

groan

BTW, may I suggest simplifying the code a bit with a While loop instead?

    SeparateJSON_RegexMatch = SeparateJSON_Regex.Search(InputPayload)
    while SeparateJSON_RegexMatch <> nil
      SeparateJSONRegEx_HitText = SeparateJSON_RegexMatch.SubExpressionString(0)
      TargetInfoInputQueue.Append SeparateJSONRegEx_HitText
      SeparateJSON_RegexMatch = SeparateJSON_Regex.Search
    wend 

Thanks Kem. Is there any technical efficiencies with it or just quicker/cleaner code?

Thanks!

It’s cleaner and more efficient. There is only one test for nil instead of two.

Thank you Kem!