I'm learning how to make use of regex. I want to parse a text file and get the data after a pattern match is found.
But here's the problems I am having:
* Only the 1st word after the pattern match gets returned.
* Employee's Name: and Last Time: doesn't get returned.
* The beginning and end of the file gets returned, ie
Status: Connected and Status: Signed, I don't need this.
Here's what my text file looks like.
##########################
### Status: Connected
4/9/2
Employee's Name: john doe
Address: 1234 nowhere street
Telephone: 555-555-5555
Last Time: 12:00 pm
4/9/2
Employee's Name: john doe
Address: 1234 nowhere street
Telephone: 555-555-5555
Last Time: 12:00 pm
### Status: Signed Off.
#######################
But here's the problems I am having:
* Only the 1st word after the pattern match gets returned.
* Employee's Name: and Last Time: doesn't get returned.
* The beginning and end of the file gets returned, ie
Status: Connected and Status: Signed, I don't need this.
Here's what my text file looks like.
##########################
### Status: Connected
4/9/2
Employee's Name: john doe
Address: 1234 nowhere street
Telephone: 555-555-5555
Last Time: 12:00 pm
4/9/2
Employee's Name: john doe
Address: 1234 nowhere street
Telephone: 555-555-5555
Last Time: 12:00 pm
### Status: Signed Off.
#######################
Code:
Dim text As String = "C:\test.txt"
Dim sr As StreamReader = New StreamReader(text)
text = sr.ReadToEnd
sr.Close()
Dim pat As String = "(\w+):\s(\w+)?\b"
' Compile the regular expression.
Dim r As New Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
While m.Success
Dim cc As CaptureCollection = m.Captures
Dim c As Capture
For Each c In cc
System.Console.WriteLine(c.ToString())
Next c
' Advance to the next match.
m = m.NextMatch()
End While