Forum Discussion

mustak_m's avatar
mustak_m
Occasional Contributor
10 years ago

How to get the matched string









I am using StrMatches method to find if string contains sub string using regular expression. StrMatches returns True if string matches else False. Now I want to retrieve the matched string. For ex.

sString = "Test String:Whats going on"

sRegExpr = ".*Whats go.*"

Utility_StrMatches = aqString.StrMatches("^" & sRegExpr & "$",sString)



This will return True. How to retrieve the extact matched string. For above ex. code snippet should return "String:Whats going".





Example 2.

sString = "Test String:Whats going on"

sRegExpr = "go.*g"



Expected output: "going"





Example 3.

sString = "Test String:Whats going on"

sRegExpr = "Test .* on"



Expected output: "Test String:Whats going on"




  • Hi Mustak,



    aqString.StrMatches doesn't capture matches. You need to use the language's native regular expressions for that. For example, in VBScript, you need to use the RegExp.Execute method.



    Sub Test

      Dim sString, re, colMatches



      sString = "Test String:Whats going on"

      Set re = New RegExp

      re.Pattern = "go.*g"



      Set colMatches = re.Execute(sString)

      If colMatches.Count > 0 Then

        Log.Message colMatches(0).Value

      Else

        Log.Message "Match not found"

      End If

    End Sub


  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)


    Hi Mustak,



    aqString.StrMatches doesn't capture matches. You need to use the language's native regular expressions for that. For example, in VBScript, you need to use the RegExp.Execute method.



    Sub Test

      Dim sString, re, colMatches



      sString = "Test String:Whats going on"

      Set re = New RegExp

      re.Pattern = "go.*g"



      Set colMatches = re.Execute(sString)

      If colMatches.Count > 0 Then

        Log.Message colMatches(0).Value

      Else

        Log.Message "Match not found"

      End If

    End Sub