Solved

Using Regex to parse WSUS update list for KB Articles.

Posted on 2011-03-20
2
598 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now