我编写了这个非常直接的regex代码。
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string lines = "2019-05-07 11:50:28, INFO Ayush Singhania, Level 1, EID: 1001, UID: ayush.power, Message: Create a Job";
Regex pattern = new Regex(@"(?<Date>.*?\s)(?<Time>.*?), INFO(?<Name>.*?),(?<Level>.*?), EID: (?<EID>.*?), UID: (?<UID>.*?), Message: (?<Message>.*?)");
MatchCollection myMatchCollection = pattern.Matches(lines);
foreach (Match myMatch in myMatchCollection)
{
var Date = myMatch.Groups["Date"].Value;
var Time = myMatch.Groups["Time"].Value;
var Name = myMatch.Groups["Name"].Value;
var Level = myMatch.Groups["Level"].Value;
var EID = myMatch.Groups["EID"].Value;
var UID = myMatch.Groups["UID"].Value;
var Message = myMatch.Groups["Message"].Value;
Console.Out.WriteLine(Date);
Console.Out.WriteLine(Time);
Console.Out.WriteLine(Name);
Console.Out.WriteLine(Level);
Console.Out.WriteLine(EID);
Console.Out.WriteLine(UID);
Console.Out.WriteLine(Message);
}
}
}
}以下内容的输出如下:
2019-05-07
11:50:28
艾乌什·辛加尼亚
1级
1001
ayush.power
.(最后一行是空白)
对于最后一组来说,一切都很好--“消息”。
它不印任何东西。有人能说出原因吗?
而且我认为我的正则表达式是正确的。看看这里,https://regex101.com/r/ysawNT/1
发布于 2019-05-08 16:29:57
除了使用错误的消息Groups["Message"] (应该是MSG )之外,最后一部分是空的,因为最后一部分没有边界集,而非贪婪的.*?也满足于不匹配。
您可以做的是使用(?<MSG>.*)匹配其余的字符串。
Regex演示 x- C#演示

发布于 2019-05-08 16:28:24
你的小组应该是MSG
var Message = myMatch.Groups["MSG"].Value;https://stackoverflow.com/questions/56045139
复制相似问题