[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get contents of text with regex

Posted on 2011-05-09
12
Medium Priority
?
289 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Michael Roodt
  • 6
  • 6
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35720947
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
 
LVL 2

Author Comment

by:Michael Roodt
ID: 35721058
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35721085
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:Michael Roodt
ID: 35721132
Yes, that's what I mean by a block
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35721281
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35721292
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
 
LVL 2

Author Comment

by:Michael Roodt
ID: 35721417
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
 
LVL 2

Author Comment

by:Michael Roodt
ID: 35727386
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
 
LVL 2

Author Comment

by:Michael Roodt
ID: 35728973
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35729157
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35729220
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
 
LVL 2

Author Closing Comment

by:Michael Roodt
ID: 35729933
Been a really great help. Thanks
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question