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

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&&&&&&
Mike MillerSoftware EngineerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CodeCruiserConnect With a Mentor Commented:
Try

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

Replace the text in StartsWith and EndsWith with whatever you want
0
 
Julian HansenCommented:
Try this
Literally
Dim regex As Regex = New Regex("[\$]{7}.*[\&]{6}")
Dim match As Match = regex.Match(input)
If match.Success Then
	    Console.WriteLine(match.Value)
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
	    Console.WriteLine(match.Value)
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.
0
All Courses

From novice to tech pro — start learning today.