Creating a string from partial contents of a text file in VB.NET

Posted on 2012-08-22
Last Modified: 2012-08-24
How would I read a text file and assign a value in between 2 sets of text to a string.

i.e. (Text File contents)

the bird flew to the nest
$$$$$$$the dog ran to the field&&&&&&
the cat walked to the pond

If the 2 sets of text I wanted to search for were $$$$$$$ and &&&&&&, the value of the string produced would be:

$$$$$$$the dog ran to the field&&&&&&
Question by:mwmiller78
    LVL 49

    Expert Comment

    by:Julian Hansen
    Try this
    Dim regex As Regex = New Regex("[\$]{7}.*[\&]{6}")
    Dim match As Match = regex.Match(input)
    If match.Success Then
    End If

    Open in new window

    If the $$ is variable then
    Dim regex As Regex = New Regex("[\$]+.*[\&]+")
    Dim match As Match = regex.Match(input)
    If match.Success Then
    End If

    Open in new window

    If prefix and suffix are not exactly as you described then modify regex to match what you are looking for.
    LVL 83

    Accepted Solution


    For Each Line As String In IO.File.ReadAllLines("textfilepath")
          If Line.StartsWith("$$$$$...") AND Line.EndsWith("&&&&...") Then
              msgbox Line
          End If

    Replace the text in StartsWith and EndsWith with whatever you want

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    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…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now