如果你想使用正则表达式,你可以稍微修改你当前的正则表达式,因为$ 0组具有你正在寻找的东西。
BEGIN:VBODY\n?((?:[^\n]*\n+)+?)END:VBODY
基本上发生了什么事([^\n]*\n+)+
变成(?:[^\n]*\n+)+?
(把这个部分懒惰可能是更安全)
然后环绕括号是整体的一部分:((?[^\n]*\n+)+?)
我加入\n?
在此之前,使输出一点清洁剂。
非正则表达式的解决方案可能是这样的:
string str = @"N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG";
int startId = str.IndexOf("BEGIN:VBODY")+11; // 11 is the length of "BEGIN:VBODY"
int endId = str.IndexOf("END:VBODY");
string result = str.Substring(startId, endId-startId);
Console.WriteLine(result);
输出:
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
ideone demo
我不认为你需要使用正则表达式。在BEGIN之后收集任何行很简单:VBODY被看到,直到行结束为止:VBODY – Spaceghost
嘿Spaceghost,你有什么样的c#代码样本会是什么样子 – d123