Link to home
Start Free TrialLog in
Avatar of Michael Roodt
Michael Roodt

asked on

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
Avatar of Michael Roodt
Michael Roodt

ASKER

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

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

Yes, that's what I mean by a block
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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
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

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

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

Been a really great help. Thanks