Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Strip out text between two tags in VB.net

Posted on 2007-08-07
Medium Priority
Last Modified: 2009-07-29
Dear Experts,

I'm trying to find a way to strip out text from a string between two tags in vb.net. For example:

<@ACCOUNT1> AND ClientAccounts.Account_ID=@@Account_ID) </@ACCOUNT1>

<@ACCOUNT1> = Opening tags
</@ACCOUNT1> = Closing tag

If anybody could provide help I'd appreciate it.


Question by:nkewney
LVL 38

Expert Comment

ID: 19645645
Option Explicit

Private Sub Command1_Click()
    Dim strTest As String
    strTest = "<@ACCOUNT1> AND ClientAccounts.Account_ID=@@Account_ID) </@ACCOUNT1>"
    MsgBox fParseBetween(strTest, "<@ACCOUNT1>", "</@ACCOUNT1>")
End Sub

Public Function fParseBetween(ByVal strChk As String, ByVal strFirst As String, ByVal strLast As String, Optional lngStart = 1) As String
    Dim lngPos As Long, lngLen As Long
    lngPos = InStr(lngStart, strChk, strFirst, vbTextCompare) + Len(strFirst)
    lngLen = InStr(lngPos, strChk, strLast, vbTextCompare) - lngPos
    If lngPos > 0 And lngLen > 0 Then
        fParseBetween = Mid$(strChk, lngPos, lngLen)
        If Len(fParseBetween) > 0 Then
            lngStart = lngStart + Len(fParseBetween)
        End If
    End If
End Function
LVL 53

Expert Comment

ID: 19645691
Do you want to return what's between the 2 tags ?

Dim myNewString = "<@ACCOUNT1> AND ClientAccounts.Account_ID=@@Account_ID) </@ACCOUNT1>"

LVL 38

Expert Comment

ID: 19645695
Sorry, that's not VB.NET...
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 53

Expert Comment

ID: 19645800
>> Sorry, that's not VB.NET...

What is not vb.net ? I know that the way I did it is a quick and dirty solution (not with functions... - and Dim myNewString  must be Dim myNewString  as string)
LVL 38

Accepted Solution

PaulHews earned 2000 total points
ID: 19645898
Dhaest, my post above was not .NET... :) I didn't even see your comment before I posted the second comment.

 Public Function ParseBetween(ByVal ToParse As String, ByVal StartString As String, ByVal EndString As String, Optional ByRef StartPos As Integer = 0) As String
    Dim Pos, Pos2 As Integer
    Dim ReturnString As String = String.Empty
    Pos = ToParse.IndexOf(StartString, StartPos, System.StringComparison.CurrentCultureIgnoreCase) + StartString.Length
    If Pos >= StartString.Length Then
        Pos2 = ToParse.IndexOf(EndString, StartPos, System.StringComparison.CurrentCultureIgnoreCase)
        If Pos2 > 0 Then
            ReturnString = ToParse.Substring(Pos, Pos2 - Pos)
            StartPos = Pos2 + EndString.Length
            StartPos = 0
        End If
        StartPos = 0
    End If
    Return ReturnString

End Function
LVL 64

Expert Comment

by:Fernando Soto
ID: 19646574
Hi nkewney;

Here is some sample code using Regular Expressions to get the results you need.

Imports System.Text.RegularExpressions

        ' Test string
        Dim input As String = "<@ACCOUNT1> AND ClientAccounts.Account_ID=@@Account_ID) </@ACCOUNT1>"
        ' Holds the extracted string
        Dim output As String = String.Empty
        Dim re As New Regex("(?i)<@ACCOUNT1>(.*?)</@ACCOUNT1>")
        Dim mc As MatchCollection = re.Matches(input)

        If mc.Count > 0 Then
            For Each m As Match In mc
                ' The value of m.Groups(1).Value holds the extracted string
        End If


Author Comment

ID: 19646774
Thank you for this. It's spot on!


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses

581 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