C# 如何使用正则表达式从字符串中检索信息

C# 如何使用正则表达式从字符串中检索信息,c#,regex,C#,Regex,我有下面的日志行格式,粗体部分在每行之间变化,其余部分是一种模式(当然行号和时间也在变化,但不相关) 第1732行:2014-10-12 09:21:26672调试[Default_Thread_7]file.name.path.location-[TestStrinnSys/1]特定通知来自网关的消息 我希望能够从这种精确格式的行中检索“Sys”、数字“1”和“SpecificNotification”,它们正在逐行更改变量。您可以使用正则表达式。将与以下正则表达式匹配: (\w+)\/(\d

我有下面的日志行格式,粗体部分在每行之间变化,其余部分是一种模式(当然行号和时间也在变化,但不相关)

第1732行:2014-10-12 09:21:26672调试[Default_Thread_7]file.name.path.location-[TestStrinnSys/1]特定通知来自网关的消息


我希望能够从这种精确格式的行中检索“Sys”、数字“1”和“SpecificNotification”,它们正在逐行更改变量。

您可以使用
正则表达式。将
与以下正则表达式匹配:

(\w+)\/(\d+)\]\s+(\w+)
代码:

用于捕获所需的字符。稍后,您可以通过引用捕获的角色

输出:

Sys
1
SpecificNotification

@单面体微观优化!谢谢,我如何添加分组?要从变量?@user2878881检索信息,请立即查看
String input = @"Line 1732: 2014-10-12 09:21:26,672 DEBUG [Default_Thread_7] file.name.path.location - [TestStrinn Sys/1] SpecificNotification message arrived from Gateway";
Regex rgx = new Regex(@"^\s*Line\s*\d+:\s*.*?\s*file\.name\.path\.location\s*-\s*\[\s*\S+\s*([^\/]*)\/(\d+)\]\s*(\S+)");
foreach (Match m in rgx.Matches(input))
{
    Console.WriteLine(m.Groups[1].Value);
    Console.WriteLine(m.Groups[2].Value);
    Console.WriteLine(m.Groups[3].Value);
}
Sys
1
SpecificNotification