Using Regex to parse WSUS update list for KB Articles.



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.
LVL 15
Russell_VenableAsked:
Who is Participating?
 
enachemcCommented:
(KB\d+)
0
 
Russell_VenableAuthor Commented:
Seriously? lol. That is so easy! Thanks! I totally overlooked adding a +. Good pointing that out!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.