Link to home
Start Free TrialLog in
Avatar of Joel Allen
Joel Allen

asked on

Parsing Text

Hello,

I'm trying to parse out some text from a string using the Len, Right, and InStrRev function.  I just can't get it right and am asking for help.  The string would be something like this:

"My Printer Redirected (123)"

I'm trying to capture "(123)".  This line always changes.  The only text that is the same every time is the text "Redirected".  So in this case:

"blah blah blah Redirected (99)"

I would need to capture the "(99)" this time.  I basically need to capture all text on the right side of "Redirected".

Thanks for your help,
Joel
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany 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
Hi,

Regex are your friends:
Public Function GetText(ByVal data As String) As String
    Dim rx As Object
    Set rx = CreateObject("VBScript.RegExp")

    rx.Global = True
    rx.MultiLine = False
    rx.Pattern = "^.*Redirected (\(\d+\))$"

    GetText = rx.Replace(data, "$1")
End Function

Public Sub test()
    Dim data As String
    data = GetText("blah blah blah Redirected (99)")
    
    Debug.Print data    '// print: (99)
End Sub

Open in new window

Avatar of Joel Allen
Joel Allen

ASKER

Thank you.