How to replace a string that is enclosed for a couple of tags

Posted on 2011-10-25
Last Modified: 2012-05-12

Now I have a string like this "start-blablablablablabla-end" in a file, I need to replace the whole string but the deal is that the string between "start-" and "-end", is not always the same (content and size).

Question by:dimensionav
    LVL 14

    Expert Comment

    Assume your text string is in the declared text variable "txt".

    Dim NewText As String

    If Left(txt, 6) = "start-" And Right(txt, 4) = "-end" Then
       'do your string substitution here
       NewText = <whatever>
    End If
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    I would look at regular expressions.

    My straight VB is pretty rusty but check out:

    You would want to replace:  "(start-).*(-end)" with whatever you needed.

    >>If Left(txt, 6) = "start-" And Right(txt, 4) = "-end" Then

    From the related question I believe that string can exist anywhere in a larger string.
    LVL 17

    Accepted Solution

    Sub Macro1()
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim fileStream
    Dim s
    Dim ifrom
    Dim ito
    Dim sToReplace
    Dim sToBeReplaced

    Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 1)
    sToReplace = "just a test string"

    s = fileStream.ReadAll
    ifrom = InStr(1, s, "start-", vbTextCompare) + Len("start-")
    ito = InStr(1, s, "-end", vbTextCompare)
    sToBeReplaced = Mid(s, ifrom, ito - ifrom)
    s = Replace(s, sToBeReplaced, sToReplace)

    Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 2)
    fileStream.Write (s)
    End Sub
    LVL 44

    Expert Comment

    by:Martin Liss
    Const TESTDATA = "start-blablablablablabla-end"
    Dim intStart As Integer
    Dim intEnd As Integer
    intStart = InStr(TESTDATA, "start-")
    intEnd = InStr(TESTDATA, "-end")
    Debug.Print Mid$(TESTDATA, intStart + Len("start-"), intEnd - intStart - Len("start-"))

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    779 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