Micro Focus QTP (UFT) Forums
regex works but how do i return the found string to a variable - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Regular Expressions (https://www.learnqtp.com/forums/Forum-UFT-QTP-Regular-Expressions)
+--- Thread: regex works but how do i return the found string to a variable (/Thread-regex-works-but-how-do-i-return-the-found-string-to-a-variable)



regex works but how do i return the found string to a variable - jove1776 - 08-24-2011

Maybe a simple question, so apologies in advance.

I have the following:

Code:
response ="blablahTO-001-A-01blahblablah"

Set oRegExp = New RegExp
oRegExp.IgnoreCase = True
oRegExp.Global=False
oRegExp.Pattern="([A-Z]{2})-([0-9]{3})-([A-Z]{1})-([0-9]{2})"

Msgbox oRegExp.Test(response)

I get a popup saying that it is found

i presume that the correct way to get the string is something like

Code:
string = oRegExp.Test(response)
this sets string to true, but i want string to be set to TO-001-A-01

but I don't know which function will return the found pattern?

Does anyone know how to assign the found pattern to the variable?


RE: regex works but how do i return the found string to a variable - jove1776 - 08-24-2011

Ok I managed to get it to work using the following I found on the net:

Code:
Dim re, testresults, showresults

Set re = new regexp
re.Pattern = "([A-Z]{2})-([0-9]{3})-([A-Z]{1})-([0-9]{2})"
re.Global = true
re.IgnoreCase = true

set testresults = re.Execute(response)
For each match in testresults
    showresults = showresults + match.value
Next

msgbox showresults

Unfortunately I didn't realize in the string i was searching there are 28 separate matches for that search which are all valid but all different.

I just need the very last instance of the match for my purposes.

Does anyone know what the regex is for this?

I am a bit new to regex





RE: regex works but how do i return the found string to a variable - jove1776 - 08-24-2011

I managed to solve the issue by using the string function right to cut down the string. I then used the regex to search for that pattern.

i bet you could do this with regex to match the last instance, please let me know if it is possible, as my code is a bit of a beginners messy code Smile