Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Vbscript to Extract text within a String

Posted on 2012-03-29
7
Medium Priority
?
1,473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 58

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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Progress
Suggested Courses

618 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