Solved

Vbscript to Extract text within a String

Posted on 2012-03-29
7
1,167 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Select specific duplicate row based on specific criteria 4 56
Create Consolidation Sheet 3 38
Generate Unique ID in VB.NET 21 54
Create .bat File 16 30
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

840 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