Solved

Vbscript to Extract text within a String

Posted on 2012-03-29
7
1,094 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 51

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now