使用.NET Regex解析当前安装的软件包的WSUS更新

时间:2011-03-20 00:41:00

标签: c# .net regex vb.net

问题:
当前正则表达式模式不会过滤所有行。将^添加到开头,将$添加到模式的结尾似乎也会破坏它。如果我在http://www.regexlib.com上尝试它,它会使用选项(多行和不区分大小写)给出部分结果。在应用程序中使用它什么都不返回。

问题:

  1. 为什么这个正则表达式不起作用 所有行和部分给出结果。
  2. 我认为这是.NET正则表达式 发动机。为什么它在那里工作 不在这里???
  3. 我错过了正确的语法或是 模式只是缺少一些东西 关键??正则表达式总让我疯狂......
  4. //格式:(KB980218)
    //
    //示例:
    用于基于x64的系统的Windows 7安全更新(KB2479943)
    适用于基于x64的系统的Windows 7安全更新程序(KB2479943)
    Windows恶意软件删除工具x64 - 2011年3月(KB890830)
    Microsoft Silverlight更新(KB2495644)
    适用于基于x64的系统的Windows 7安全更新程序(KB2483614)
    适用于基于x64的系统的Windows 7的Internet Explorer 8累积安全更新(KB2482017)
    适用于基于x64的系统的Windows 7安全更新程序(KB2425227)
    Windows恶意软件删除工具x64 - 2011年2月(KB890830)
    适用于基于x64的系统的Windows 7安全更新程序(KB2479628)
    适用于基于x64的系统的Windows 7更新(KB2467023)
    Windows Live Essentials 2011(KB2434419)
    适用于基于x64的系统的Windows 7更新(KB2454826)
    适用于基于x64的系统的Windows 7安全更新程序(KB2475792)
    适用于基于x64的系统的Windows 7安全更新程序(KB2393802)
    适用于基于x64的系统的Windows 7安全更新程序(KB2485376)
    适用于基于x64的系统的Windows 7更新(KB976902)
    Windows恶意软件删除工具x64 - 2011年1月(KB890830)
    适用于基于x64的系统的Windows 7安全更新程序(KB2419640)
    适用于基于x64的系统的Windows 7的Internet Explorer 8兼容性视图列表更新(KB2447568)
    适用于基于Windows 7 x64的系统的Media Center累积更新(KB2284742)
    Microsoft Silverlight更新(KB2477244)

    测试此函数会返回标题,但不会返回kb文章编号。 ================================================== ================================
    标题:适用于基于x64的系统的Windows 7安全更新程序(KB2479943)
    KB#:
    ================================================== ================================
    标题:Windows恶意软件删除工具x64 - 2011年3月(KB890830)
    KB#:
    ================================================== ================================
    标题: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;
    
    }
    

    上面使用这种方法我什么都没得到。在http://www.regexlib.com在线正则表达式测试仪上使用以下模式来获取.net我只得到部分结果。

    模式的匹配:(((\ w {2} \ d {6})?)

    Match   $1  $2
    (KB890830)  (KB890830)  (KB890830)
    (KB890830)  (KB890830)  (KB890830)
    (KB976902)  (KB976902)  (KB976902)
    (KB890830)  (KB890830)  (KB890830)
    

2 个答案:

答案 0 :(得分:1)

首先,请注意,某些KB数字有六位数,有些有七位数。单独匹配KB编号的表达式可以是

\(KB\d{6,7}\)

我认为这解释了为什么你的代码匹配某些行而不是其他行。这也适用于带有CaseInsensitive和Multiline的regexlib测试仪。请注意,您需要转义模式中的括号,否则它们具有特殊含义(用于分隔捕获组)。

我希望能回答你的问题。无论如何,看起来您需要检查方法的逻辑,因为您有

foreach (Match match in matches)
    result = match.Value;
return result;

这没有多大意义。

答案 1 :(得分:0)

最终所需的模式是(KB \ d +)。不管怎样,谢谢。