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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

C# Regular Expression help.

Hi,

I am trying to get my regular expression in C# to extract the following

<![LOG[==========[ ccmsetup started in process 3172 ]==========]LOG]!><time="14:32:31.717-600"{ date="07-29-2013" component="ccmsetup" context="" type="1" thread="908" file="ccmsetup.cpp:9100">


so it would gather the following:

Capture 1: ==========[ ccmsetup started in process 3172 ]==========
Capture 2: 14:32:31.717-600
Capture 3: 07-29-2013

The problem happens if I have a multi-line entry:

<![LOG[Running installation package
  Package:     C:\Windows\ccmsetup\MicrosoftPolicyPlatformSetup.msi
  Log:         C:\Windows\ccmsetup\Logs\MicrosoftPolicyPlatformSetup.msi.log
  Properties:  REBOOT=Suppress ALLUSERS=1]LOG]!><time="14:33:26.552-600" date="07-29-2013" component="ccmsetup" context="" type="1" thread="908" file="msiutil.cpp:791">


I have got the following regular expression which almost works - except the new line.


<!\[LOG\[(.*)]LOG]!><time="(\d{1,2}:\d{1,2}:\d{1,2}.\d{1,3}-\d{1,3})"\sdate="(\d{1,2}-\d{1,2}-\d{1,4})"


Any suggestions on how to change the expression to make it work.


Thanks,

Ward.
0
whorsfall
Asked:
whorsfall
1 Solution
 
Robert SchuttSoftware EngineerCommented:
Try this:
Regex re = new Regex(@"<!\[LOG\[(.*)]LOG]!><time=""(\d{1,2}:\d{1,2}:\d{1,2}\.\d{1,3}-\d{1,3})""\sdate=""(\d{1,2}-\d{1,2}-\d{1,4})""", RegexOptions.Singleline);

Open in new window

The option SingleLine changes matching so that a period matches any character, including a newline which it normally doesn't match.

The code for the function I used to test this and provide the output you specified:
        private void doTest(string s) {
            //Regex re = new Regex("<!\\[LOG\\[(.*)]LOG]!><time=\"(\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}-\\d{1,3})\"\\sdate=\"(\\d{1,2}-\\d{1,2}-\\d{1,4})\"", RegexOptions.Singleline);
            Regex re = new Regex(@"<!\[LOG\[(.*)]LOG]!><time=""(\d{1,2}:\d{1,2}:\d{1,2}\.\d{1,3}-\d{1,3})""\sdate=""(\d{1,2}-\d{1,2}-\d{1,4})""", RegexOptions.Singleline);
            MatchCollection ms = re.Matches(s);
            int gi = -1;
            foreach (Match m in ms) {
                foreach (Group g in m.Groups) {
                    if (++gi > 0) {
                        Console.WriteLine("Capture {0}: {1}", gi, g.Value);
                    }
                }
            }
        }

Open in new window

This is the code that calls that function:
          //string s1 = @"<![LOG[==========[ ccmsetup started in process 3172 ]==========]LOG]!><time=""14:32:31.717-600""{ date=""07-29-2013"" component=""ccmsetup"" context="""" type=""1"" thread=""908"" file=""ccmsetup.cpp:9100"">";
          //                                                                                                              ^- typo here
            string s1 = @"<![LOG[==========[ ccmsetup started in process 3172 ]==========]LOG]!><time=""14:32:31.717-600"" date=""07-29-2013"" component=""ccmsetup"" context="""" type=""1"" thread=""908"" file=""ccmsetup.cpp:9100"">";
            string s2 = @"<![LOG[Running installation package
  Package:     C:\Windows\ccmsetup\MicrosoftPolicyPlatformSetup.msi
  Log:         C:\Windows\ccmsetup\Logs\MicrosoftPolicyPlatformSetup.msi.log
  Properties:  REBOOT=Suppress ALLUSERS=1]LOG]!><time=""14:33:26.552-600"" date=""07-29-2013"" component=""ccmsetup"" context="""" type=""1"" thread=""908"" file=""msiutil.cpp:791"">";

            Console.WriteLine("Test 1");
            doTest(s1);
            Console.WriteLine("Test 2");
            doTest(s2);

Open in new window

Note: there's a typo in your "control" string (indicated in the comment within the code above) which caused a mismatch.

Here's a screen capture of the output:
capture of output
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now