How to get contents of text with regex

I'm trying to find a way of searching through a text file, locate  where it says JMAPROD 1.7 (2011-05-09 and then store the results to an arraylist.

I don't really know regular expressions but I think special characters are causing confusion. I'm not sure if thats the right regex to use. The year,month and day are all variables I get from date.now in vb.net. They are string variables.

I would appreciate any help on this.




***********************************************
*      JMAPROD 1.7 (2011-05-09T03:00:00)      *
***********************************************
JMAPROD: Processing seq. number 353
JMAPROD: Updating JMNPLU.FIL..
JMAPROD: Reading RM product data..
JMAPROD: Color conversions (JMACMAP)
JMAPROD: Size conversions (JMASIZE)
JMAPROD: Brand descriptions (JMADESC)
JMAPROD: Amazon categories (JMACATS)
JMAPROD: EAN13 table (JMNPLU)
JMAPROD: Creating Amazon Product Feed
JMAPROD: Creating Amazon Relationship Feed
JMAPROD: Creating Amazon Image Feed
JMAPROD: Creating Amazon Pricing Feed
JMAPROD: Resetting Amazon Flags
JMAPROD: Job Complete.....  2012-05-09T03:00:10
Dim a As Match = Regex.Match(sContents, "(?<=JMAPROD 1.7 (" + year + "-" + Month + "-" + day + " * ))\d+")

        Dim s As New ArrayList

        Do While a.Success
            s.Add(a.Value)

            a = a.NextMatch()

        Loop
        a = a.NextMatch()

Open in new window

LVL 2
Michael RoodtSenior .NET Integrations DeveloperAsked:
Who is Participating?
 
käµfm³d 👽Commented:
How about this algorithm? At the end, you will have a Dictionary with the Date as the key, and the "block" as the value.
Dim jobs As New Dictionary(Of DateTime, String)

Using reader As New System.IO.StreamReader("input.txt")
    Dim temp As New System.Text.StringBuilder()
    Dim tempDate As DateTime
    Dim isJobComplete As Boolean = False

    While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "JMAPROD\s*[\d.]+\s*\((\d+-\d+-\d+)")

        If m.Success Then
            tempDate = Convert.ToDateTime(m.Groups(1).Value)
        ElseIf line.StartsWith("*") Then
            If temp.Length > 0 AndAlso isJobComplete Then
                jobs.Add(tempDate, temp.ToString())
                isJobComplete = False
                temp.Length = 0
            End If
        Else
            If line.Contains("Job Complete") Then
                isJobComplete = True
            End If

            temp.AppendLine(line)
        End If
    End While

    If temp.Length > 0 AndAlso isJobComplete Then
        jobs.Add(tempDate, temp.ToString())
    End If
End Using

Open in new window

0
 
käµfm³d 👽Commented:
You need to escape the parentheses. Also, I don't see where you're accounting for the time portion of the date (or the "T").

What are you trying to extract?
0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Thanks,

I don't need to specify the time, just the date really ( Thats why I ignore the time), basically the code below, I lwant to look at each block and see if the jobs runs successfully by seeing if the  text " Job complete..... " exisits in the block. I was trying to use regex to grab each match, add the block  of text to  a string variable, then add the string to an array so I can loop through the results and do as I please i.e  show it in a gridview  and so on.

I'm open to suggestions if there is a more efficient  / easier way.

Thanks
***********************************************
*      JMAPROD 1.7 (2011-05-06T11:55:32)      *
***********************************************
JMAPROD: Processing seq. number 351
JMAPROD: Updating JMNPLU.FIL..
JMAPROD: Reading RM product data..
JMAPROD: Color conversions (JMACMAP)
JMAPROD: Size conversions (JMASIZE)
JMAPROD: Brand descriptions (JMADESC)
JMAPROD: Amazon categories (JMACATS)
JMAPROD: EAN13 table (JMNPLU)
JMAPROD: Creating Amazon Product Feed
JMAPROD: Creating Amazon Relationship Feed
JMAPROD: Creating Amazon Image Feed
JMAPROD: Creating Amazon Pricing Feed
JMAPROD: Resetting Amazon Flags
JMAPROD: Job Complete.....  2012-05-06T11:55:44
***********************************************
*      JMAPROD 1.7 (2011-05-07T03:00:01)      *
***********************************************
JMAPROD: Processing seq. number 352
JMAPROD: Updating JMNPLU.FIL..
JMAPROD: Reading RM product data..
JMAPROD: Color conversions (JMACMAP)
JMAPROD: Size conversions (JMASIZE)
JMAPROD: Brand descriptions (JMADESC)
JMAPROD: Amazon categories (JMACATS)
JMAPROD: EAN13 table (JMNPLU)
JMAPROD: Creating Amazon Product Feed
JMAPROD: Creating Amazon Relationship Feed
JMAPROD: Creating Amazon Image Feed
JMAPROD: Creating Amazon Pricing Feed
JMAPROD: Resetting Amazon Flags
JMAPROD: Job Complete.....  2012-05-07T03:00:17
***********************************************
*      JMAPROD 1.7 (2011-05-09T03:00:00)      *
***********************************************
JMAPROD: Processing seq. number 353
JMAPROD: Updating JMNPLU.FIL..
JMAPROD: Reading RM product data..
JMAPROD: Color conversions (JMACMAP)
JMAPROD: Size conversions (JMASIZE)
JMAPROD: Brand descriptions (JMADESC)
JMAPROD: Amazon categories (JMACATS)
JMAPROD: EAN13 table (JMNPLU)
JMAPROD: Creating Amazon Product Feed
JMAPROD: Creating Amazon Relationship Feed
JMAPROD: Creating Amazon Image Feed
JMAPROD: Creating Amazon Pricing Feed
JMAPROD: Resetting Amazon Flags
JMAPROD: Job Complete.....  2012-05-09T03:00:10

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
käµfm³d 👽Commented:
add the block  of text to  a string variable
So given the post above, a "block" you would add to your array would, for example, be this?
JMAPROD: Processing seq. number 351
JMAPROD: Updating JMNPLU.FIL..
JMAPROD: Reading RM product data..
JMAPROD: Color conversions (JMACMAP)
JMAPROD: Size conversions (JMASIZE)
JMAPROD: Brand descriptions (JMADESC)
JMAPROD: Amazon categories (JMACATS)
JMAPROD: EAN13 table (JMNPLU)
JMAPROD: Creating Amazon Product Feed
JMAPROD: Creating Amazon Relationship Feed
JMAPROD: Creating Amazon Image Feed
JMAPROD: Creating Amazon Pricing Feed
JMAPROD: Resetting Amazon Flags
JMAPROD: Job Complete.....  2012-05-06T11:55:44

Open in new window

0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Yes, that's what I mean by a block
0
 
käµfm³d 👽Commented:
The above is based on the assumption that you won't have two "blocks" on the same date. If you do, we can tweak the above to accommodate.
0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Wow, I'll give that a go, no chance i would have thought of that. Still learning the ropes. I'll let you know, thanks for the help
0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Thanks Kaufmed, that works brilliantly, I've not used dictionary before and have found it  useful. I thought  there wouldn't be duplicate dates but I noticed I do on the odd occasion when the job that produces the log has had to be run twice. Would you be able to show me how to accomodate for duplicate dates.

Cheers
0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Infact I think I need to go back on myself, I do need unique values and I think I can only do that by having the date and time as  the key in the dictionary if that's possible? So rather than  looping through multiple dates and having issues with duplicate keys, the code above is perfect but I need help on the following regular expression please.


Above you have  Regex.Match(line, "JMAPROD\s*[\d.]+\s*\((\d+-\d+-\d+)") to retreive the date from JMAPROD 1.7 (2011-05-10T03:00:00)

Is it possible I could retreive the date and time i.e. 05/10/2011 03:00:00. What is the regular expression for that?

Thanks

0
 
käµfm³d 👽Commented:
Is it possible I could retreive the date and time i.e. 05/10/2011 03:00:00. What is the regular expression for that?
Sure. Try:
System.Text.RegularExpressions.Regex.Match(line, "JMAPROD\s*[\d.]+\s*\((\d+-\d+-\d+T\d+:\d+:\d+)")

Open in new window

0
 
käµfm³d 👽Commented:
There's nothing really to say you have to convert the date value to a DateTime object either. You could just as easily leave the time as a string:
Dim jobs As New Dictionary(Of String, String)

...

Dim tempDate As String

...

If m.Success Then
            tempDate = m.Groups(1).Value

Open in new window

0
 
Michael RoodtSenior .NET Integrations DeveloperAuthor Commented:
Been a really great help. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.