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:[/one string]2/4 0
[onestring]twostring:[/one string]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
I need vbscript code to extract data within a string. Below are 2 examples of the type of string:
[onestring]twostring:[/one
[onestring]twostring:[/one
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
myString = "[onestring]twostring:[/on estring]2/ 40"
a = Split(myString, "/")
ub = UBound(a) - 1
result = a(ub)
cnt = InStr(result, "]") + 1
result = Mid(result, cnt)
MsgBox result
a = Split(myString, "/")
ub = UBound(a) - 1
result = a(ub)
cnt = InStr(result, "]") + 1
result = Mid(result, cnt)
MsgBox result
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Here is a function.
You can call it with ...
MsgBox myFunction("[onestring]two string:[/o nestring]X Y4/2")
You can call it with ...
MsgBox myFunction("[onestring]two
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
Here is a short function. You can call it with ...
tmp = "[onestring]twostring:[/on estring]XY 4/2"
a = myFunction(tmp)
MsgBox a
tmp = "[onestring]twostring:[/on
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
ASKER
All,
Thank you for responding.
The answer given by wdosanjos fits my need best.
Diammond
Thank you for responding.
The answer given by wdosanjos fits my need best.
Diammond
Mid(strTemp, instrrev(strTemp, "/")+1)
~bp