我有一个示例日志,我正在尝试找到最佳解决方案来获取信息:主要是time stamp
和特定字符串。
示例日志输出:每个都在1行
####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main>
<<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>
每个日志行都有4个开始#
标记,后跟<Timestamp>
所有与我的方案相关的部分都包含<>
个括号,我希望有一个信息数组索引基于从左到右的读数。
对于每个<>
括号,我想将其信息存储在lineArray中。
using (var reader = new StreamReader(@"C:\Projects\test.txt"))
{
foreach(var line in ReadLines(reader))
{
//add logic to parse and read info into brackets
String sLine = (String)line;
String splitD = sLine.Split("<>")[0];
}
}
答案 0 :(得分:3)
好吧,我不是一个正则表达式大师,但这应该有效:
string test = "####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main>"
+ "<<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>";
var pattern = @"\<+(.*?)\>+";
var matches = Regex.Matches(test, pattern);
foreach (Match m in matches)
{
Console.WriteLine(string.Format("-{0}-", m.Groups[1]));
}
Console.ReadKey();
输出(-
个字符之间的实际匹配):
-Jun 22, 2012 12:54:18 PM CDT-
-Notice-
-WebLogicServer-
-lname-
-dname-
-main-
-WLS Kernel-
--
-BEA-000360-
-Application started in RUNNING mode-
string test = "####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>"
+ Environment.NewLine + "####<Jun 23, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>"
+ Environment.NewLine + "####<Jun 24, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>";
List<Foo> foo = new List<Foo>();
using (StringReader reader = new StringReader(test))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string pattern = @"\<+(.*?)\>+";
var matches = Regex.Matches(line, pattern);
foo.Add(new Foo
{
Timestamp = matches[0].Groups[1].ToString(),
Field2 = matches[1].Groups[1].ToString()
});
}
}
输出:
3个Foo对象的列表,每个对象具有不同的时间戳(Jun 22,Jun 23日,6月24日)