Solved

Vbscript to Extract text within a String

Posted on 2012-03-29
7
1,126 Views
Last Modified: 2012-03-29
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
0
Comment
Question by:Diammond
7 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37783083
This should extract that text from a string called strTemp.

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

~bp
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37783102
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
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 400 total points
ID: 37783160
Here is another alternative:

s = "[onestring]twostring:[/onestring]2/40"

i = InStrRev(s, "/")
j = InStrRev(s, "]")

v1 = mid(s, j + 1, i - j - 1)
v2 = mid(s, i + 1)

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 17

Expert Comment

by:vb_elmar
ID: 37783171
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

0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37783205
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

0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37783258
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

0
 

Author Comment

by:Diammond
ID: 37783725
All,

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

Diammond
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question