Link to home
Start Free TrialLog in
Avatar of Diammond
Diammond

asked on

Vbscript to Extract text within a String

Hello,
I need vbscript code to extract data within a string.  Below are 2 examples of the type of string:

[onestring]twostring:[/onestring]2/40
[onestring]twostring:[/onestring]XY4/2

Need code that will go to the last occurrence of “/” and extract the text between it and the last occurrence of “]”.  In the two examples above, 2 and XY4 would be the output.

Need code that will go to the last occurrence of “/” and extract the text after it.  In the two examples above, 40 and 2 would be the output.

I’m sure this can be accomplished with text functions (mid, left, right, instr), but I am not sure of the parameters in each statement.  

Any help would be greatly appreciated.

Thanks
Diammond
Avatar of Bill Prew
Bill Prew

This should extract that text from a string called strTemp.

Mid(strTemp, instrrev(strTemp, "/")+1)

~bp
myString = "[onestring]twostring:[/onestring]2/40"
a = Split(myString, "/")
ub = UBound(a) - 1
result = a(ub)
cnt = InStr(result, "]") + 1
result = Mid(result, cnt)
MsgBox result
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America 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
myString = "[onestring]twostring:[/onestring]2/40"
a = Split(myString, "/")
ub = UBound(a) - 1
cnt = InStr(a(ub), "]") + 1
result = Mid(a(ub), cnt)
MsgBox result

Open in new window

Here is a function.

You can call it with ...
MsgBox myFunction("[onestring]twostring:[/onestring]XY4/2")

Function myFunction(ByVal L As String) As String
    a = Split(L, "/")
    ub = UBound(a) - 1
    cnt = InStr(a(ub), "]") + 1
    myFunction = Mid(a(ub), cnt)
End Function

Open in new window

Here is a short function. You can call it with ...

tmp = "[onestring]twostring:[/onestring]XY4/2"
a = myFunction(tmp)
MsgBox a

Function myFunction(ByVal L As String) As String
  L1 = Mid(L, InStrRev(L, "]") + 1)
  myFunction = Mid(L1, 1, InStr(1, L1, "/") - 1)
End Function

Open in new window

Avatar of Diammond

ASKER

All,

Thank you for responding.
The answer given by wdosanjos fits my need best.

Diammond