Solved

Using Regex to parse WSUS update list for KB Articles.

Posted on 2011-03-20
2
606 Views
Last Modified: 2013-12-17


This is what I am trying to filter.
        Security Update for Windows 7 for x64-based Systems (KB2479943)
        Windows Malicious Software Removal Tool x64 - March 2011 (KB890830)
        Update for Microsoft Silverlight (KB2495644)
        Security Update for Windows 7 for x64-based Systems (KB2483614)
        Cumulative Security Update for Internet Explorer 8 for Windows 7 for x64-based S
        ystems (KB2482017)
        Security Update for Windows 7 for x64-based Systems (KB2425227)
        Windows Malicious Software Removal Tool x64 - February 2011 (KB890830)
        Security Update for Windows 7 for x64-based Systems (KB2479628)
        Update for Windows 7 for x64-based Systems (KB2467023)
        Windows Live Essentials 2011 (KB2434419)
        Update for Windows 7 for x64-based Systems (KB2454826)
        Security Update for Windows 7 for x64-based Systems (KB2475792)
        Security Update for Windows 7 for x64-based Systems (KB2393802)
        Security Update for Windows 7 for x64-based Systems (KB2485376)
        Update for Windows 7 for x64-based Systems (KB976902)
        Windows Malicious Software Removal Tool x64 - January 2011 (KB890830)
        Security Update for Windows 7 for x64-based Systems (KB2419640)
        Update for Internet Explorer 8 Compatibility View List for Windows 7 for x64-bas
        ed Systems (KB2447568)
        Cumulative Update for Media Center for Windows 7 x64-based Systems (KB2284742)
        Update for Microsoft Silverlight (KB2477244)

Open in new window


and I am trying to filter for  the KB Article: (KB2477244)

The pattern I ended up working with looked like this:
(((\w{2}\d{7}) ?))

Open in new window

I am sure that there are other ways to tackle this like (((KB\d{7}) ?))

Quickly wrote this method to test this (Cover your eyes!)

 <Omitted for brevity>    
//Display WSUS results from Custom COM+ interop class     
List<string> KbUpdates = new List<string>();
Console.WriteLine("=====================================================================================");
foreach (string s in KbUpdates)
{
    string format = string.Format("Title:{0}\r\nKB#:{1}", s, GrabKBUpdate(s.ToString()));
                
    Console.WriteLine(format);
                Console.WriteLine("=====================================================================================");
 }

public string GrabKBUpdate(string updatestring)
        {
            string result = string.Empty;
            string pattern = @"(((\w{2}\d{7}) ?))", input = updatestring;
            Regex rgx = new Regex(pattern, RegexOptions.Multiline);// IgnoreCase
            MatchCollection matches = rgx.Matches(input);

            if (matches.Count > 0)
            {
                // Just wanted the first result as that is the only one that should be in the line.
                foreach (Match match in matches)
                   result = match.Value;
                return result;
                //Console.WriteLine("(Matches {0} {1}):", input, matches.Count);
                //foreach (Match match in matches)
                //    Console.WriteLine("   " + match.Value);
            }
            return result;
        }

Open in new window


Results look like this:
=====================================================================================
Title:Security Update for Windows 7 for x64-based Systems (KB2479943)
KB#:KB2479943
=====================================================================================
Title:Windows Malicious Software Removal Tool x64 - March 2011 (KB890830)
KB#:
=====================================================================================
Title:Update for Microsoft Silverlight (KB2495644)
KB#:KB2495644
=====================================================================================
Title:Security Update for Windows 7 for x64-based Systems (KB2483614)
KB#:KB2483614
=====================================================================================
Title:Cumulative Security Update for Internet Explorer 8 for Windows 7 for x64-based Systems (KB2482017)
KB#:KB2482017
=====================================================================================

Open in new window



Problems:
If you take a look at the results only the articles with 7 digits get extracted and nothing less than or greater than 7 gets extracted.
Using pattern "(((\w{2}\d{6}) ?))" returns partial results that look like this.
=====================================================================================
Title:Security Update for Windows 7 for x64-based Systems (KB2479943)
KB#:KB247994
=====================================================================================

Questions:

1. Is there a better regex pattern to filter for these or better yet a pattern plus a smoother running method? Explanations/Comments/Suggestions are welcome as well.


Conclusion:
This method will be used in a security analysis tool.
0
Comment
Question by:Russell_Venable
2 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 250 total points
ID: 35178473
(KB\d+)
0
 
LVL 15

Author Closing Comment

by:Russell_Venable
ID: 35184903
Seriously? lol. That is so easy! Thanks! I totally overlooked adding a +. Good pointing that out!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
Service Controller 1 23
ASP.NET(C#) Eliminating weekends from a date range 2 34
Help with preventing selection from a combobox 11 24
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

773 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