如果你想使用正則表達式,你可以稍微修改你當前的正則表達式,因爲$ 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