【发布时间】:2011-03-20 00:40:59
【问题描述】:
问题:
当前的正则表达式模式不会过滤所有行。在模式的开头添加 ^ 并在模式的结尾添加 $ 似乎也会破坏它。如果我在http://www.regexlib.com 上尝试它,它会使用选项(多行和不区分大小写)给出部分结果。在应用程序中使用它根本不会返回任何内容。
问题:
- 为什么这个正则表达式不起作用 所有行并部分给出结果。
- 我以为这是 .NET 正则表达式 引擎。为什么它在那里工作 不在这里???
- 是我缺少正确的语法还是 模式只是缺少一些东西 至关重要的??正则表达式总是让我发疯......
// 格式:(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)
知识库#:
==================================================== =================================
标题:Windows 恶意软件删除工具 x64 - 2011 年 3 月 (KB890830)
知识库#:
==================================================== =================================
标题:Microsoft Silverlight 更新 (KB2495644)
知识库#:
==================================================== ==================================
//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)
【问题讨论】:
-
您希望
GrabKBUpdate中的正则表达式匹配做什么?在我看来,它会匹配重复的单词,但我不明白你为什么要这样做。 -
您最后提到的另一种模式似乎缺少(至少)
d之前的反斜杠,并且括号不匹配。当你说你“只得到部分结果”时,你是什么意思?例如,您的意思是您将之前列出的所有行都输入了它,而您找到的唯一匹配项是您最后列出的四行,还是什么? -
我的错!在第一种情况下,我的模式错误,第二次我只是没有从不在项目中的记事本复制粘贴。它现在固定了。在这个页面上。
-
回答您关于 GrabKBUpdate() 的问题。它应该从字符串中返回 kbarticle。应该看起来像这样 (KB890830)。示例块在上面。