我解析文本,如果我遇到了一個時間,我想分割字符串。這裏有一個例子:爲什麼分割會消除此C#代碼中的部分字符串?
At 12:30AM I got up. At 11:30PM I went to bed.
我的代碼:
string time = @"[0-9]{2}:[0-9]{2}(A|P)M";
string test = "At 12:30AM I got up. At 11:30PM I went to bed.";
string[] result = Regex.Split(test, time);
foreach(string element in result)
{
Console.WriteLine(element);
}
我需要得到:
At 12:30AM
I got up. At 11:30PM
I went to bed.
我得到什麼:
At
A
I got up. At
P
I went to bed.
所有這一切剩下的時間是A或P.
不是'[A | P]'是否匹配'A','''和'P'? – Mehrdad 2013-02-22 05:48:55
'|'是一個特殊字符。所以它不會被匹配,除非它帶有轉義字符。 – Aditi 2013-02-22 05:53:01
不,@Mehrdad是對的:它應該是'[AP]' – 2013-02-22 05:53:22