Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions - how to get text after pattern match???

Status
Not open for further replies.

dmoonme

MIS
Joined
Jul 21, 2004
Messages
33
Location
US
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.
#######################

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
 
Try this

(?<=^[^#]+:\s).+$

It means to match characters (any, but you can change this if you like) which are preceded by the start of the line, and then some characters, but not #, and then the colon and space, and to match up to the end of the line. You'll need to set your regex options also to multi-line for this to work.

Hope this helps

Mark [openup]
 
Thanks for the reply. It works great! How can I get the field names to display too, ie. Employee's Name:, Address:, etc?
 
I think this should work for you.

^[^#]+:\s.+$

My computer is behaving a bit funny at the moment, so I can't test it. Give it a go.

Mark

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top