Strip out text between two tags in

Posted on 2007-08-07
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 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

    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

    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

    Sorry, that's not VB.NET...
    LVL 53

    Expert Comment

    >> Sorry, that's not VB.NET...

    What is not ? 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

    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 62

    Expert Comment

    by:Fernando Soto
    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

    LVL 1

    Author Comment

    Thank you for this. It's spot on!


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    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…
    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…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now