Forum Discussion

mustak_m's avatar
mustak_m
Occasional Contributor
10 years ago
Solved

RegExp.Execute method


I am using following snippet to get the matching string.



Sub ExtractDemo

Dim InStr, regEx

 

  InStr = "Description:" &_

  "Handles all actions supported " &_

 "Input Parameters:" &_

  " oInteraction: [air] Hello" &_

    " guiObject  [air]" &_

     "sObjectType" &_

     "sAction" &_

     "sActionValue" &_

 "Returns:" &_

   "PropertyValue"

  

  Set regEx = New RegExp

  regEx.Pattern = "Des.*n"

 

  Set colMatches = regEx.Execute(InStr)

  If colMatches.Count > 0 Then

    Log.Message colMatches(0).Value

  Else

    Log.Message "Match not found for: Des.*n"

  End If

End Sub



Output: Description:Handles all actions supported Input Parameters: oInteraction: [air] Hello guiObject[air]sObjectTypesActionsActionValueReturn



For reg exprs: regEx.Pattern = "^" &"Des.*n" & "$"

Output: Match not found for: Des.*n



For reg exprs: regEx.Pattern = "oIn.*n"

Output: oInteraction: [air] Hello guiObject[air]sObjectTypesActionsActionValueReturn





How can I get the exact matching string ?

Expected outputs:

1. Description

2. Description

3. oInteraction



Any thoughts


  • Hi Mustak,



    By default, * is greedy, that is, matches the longest string possible. Try this instead:



    regEx.Pattern = "Des.*?n"



    regEx.Pattern = "oIn.*?n"

1 Reply

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Mustak,



    By default, * is greedy, that is, matches the longest string possible. Try this instead:



    regEx.Pattern = "Des.*?n"



    regEx.Pattern = "oIn.*?n"