Solved

Using Regex to parse WSUS update list for KB Articles.

Posted on 2011-03-20
2
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…

733 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