好吧,我不是正则表达式专家,但这应该可行:
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 对象的列表,每个对象都有不同的时间戳(6 月 22 日、6 月 23 日、6 月 24 日)