Using .NET Regex to parse WSUS updates for currently installed packages
Problem:
Current regex pattern does not filter all lines. Adding ^ to the beginning and $ to the ending of the pattern seems to break it as well. If I try it on http://www.regexlib.com it gives partial results using options(multiline & case insensitive). Using it in the application returns nothing at all.Questions:
- Why is this regex not working with all lines and partially giving results.
- I thought this was .NET regex engine. Why does it work there and not here???
- Am I missing proper syntax or is the pattern just missing sommething crucial?? Regex always drives me crazy...
// Format: (KB980218)
// // Example: Security Update for Windows 7 for x64-based Systems (KB2479943) 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 Systems (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-based Systems (KB2447568) Cumulative Update for Media Center for Windows 7 x64-based Systems (KB2284742) Update for Microsoft Silverlight (KB2477244)Testing this function returns the title but no kb article number. ==================================================================================
Title:Security Update for Windows 7 for x64-based Systems (KB2479943) KB#: ================================================================================== Title:Windows Malicious Software Removal Tool x64 - March 2011 (KB890830) KB#: ================================================================================== Title:Update for Microsoft Silverlight (KB2495644) KB#: ==================================================================================//Main Method output example
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 static string GrabKBUpdate(string updatestring)
{
string result = null;
string pattern = @"(((\w{2}\d{6}) ?)";
string input = updatestring;
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
foreach (Match match in matches)
result = match.Value;
return result;
}
return result;
}
Using this method above I get nothing. Using the below pattern on http://www.regexlib.com online regex tester for .net I only get partial results.
Matches for pattern: (((\w{2}\d{6}) ?)Match $1 $2
(KB890830) (KB890830) (KB890830)
(KB890830) (KB890830) (KB890830)
(KB976902) (KB976902) (KB976902)
(KB890830) (KB890830) (KB890830)
First, note that some of the KB numbers have six digits, and some have seven. The expression to match the KB number alone can be simply
\(KB\d{6,7}\)
I think that explains why your code was matching some of the lines but not others. This also works for me on the regexlib tester with CaseInsensitive and Multiline. Note that you need to escape the parentheses in the pattern, otherwise they have special meaning (to delimit a capture group).
I hope that answers your questions. In any case, it looks like you need to examine the logic of your method, since you have
foreach (Match match in matches)
result = match.Value;
return result;
which doesn't make a lot of sense.
It ended up that the pattern needed was (KB\d+). Thanks anyways.
精彩评论