Use an array in a regular expression with vb.net

Good morning. Last night I attneded band practice after having practiced very little for it. I got the song list the day before and didn’t have half of them. I did okay considering it was the first time I had used a keyboard in months. I have got to keep up with that better. It was kind of fun again to be back in the saddle so to speak.

I was working on a huge text file and needed to find a faster way to run my regular expression and this did the trick. The data for teh string came from elsewhere but I hard code it here for the demo. I hope you all have a good weekend. Be sure to drop by Sunday as I am working on something out of the norm for me for that day.

Imports System.Text.RegularExpressions
Imports System.Text
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim testData As String = "The fieldwork was performed between August 1 and 5, 2003" & vbCrLf & _
            "The fieldwork was performed on August 12 and 15, 2003" & vbCrLf & _
            "The fieldwork was conducted by John Doe and Jim Public on August 12th  and 13th, 2003" & vbCrLf & _
            "The fieldwork was performed during July and August, 2003" & vbCrLf & _
            "The fieldwork was conducted by John Doe and Jim Public between March 16-29, 2004" & vbCrLf & _
            "The fieldwork was conducted in September and October 2003" & vbCrLf & _
            "The fieldwork was performed on July 24, 2003" & vbCrLf & _
            "The fieldwork was conducted on July 28 through August 15, 2003" & vbCrLf & _
            "The fieldwork was performed on July 24 and 25," & vbCrLf & _
            "The fieldwork was performed by John Doe on 07 April 2003" & vbCrLf & _
            "The fieldwork was supervised by John Doe and assisted by Jim Public from April 14 through 29, 2003"
        Dim pattern As String = "(?im)(?=.*?fieldwork\s)(?=.*?" & _
            "(?<Month>January|February|March|April|May|June|July|August|September|October|November|December))" & _
            "(?:.*?(?<Dates>\d{1,2}\s+\k<Month>.*?)|.*?(?<Dates>\k<Month>.*?))(?:\r\n|$)"
        Dim sb As StringBuilder = New StringBuilder
        Dim mc As MatchCollection = Regex.Matches(testData, pattern)
        For Each m As Match In mc
            sb.Append(m.Groups("Dates").Value & vbCrLf)
        Next
        MessageBox.Show(sb.ToString())
    End Sub

 

Consulting Requests

Feedback

Blog Front Page

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: