我怎樣才能得到這2個日期在這個字符串?從字符串獲取日期時間
從2011-08-01 00:00至2011-08-31 23:59(當地時間)2011-07-15 20:43
我想這個日期2011-08 -01 00:00和23:59 2011-08-31
我怎樣才能得到這2個日期在這個字符串?從字符串獲取日期時間
從2011-08-01 00:00至2011-08-31 23:59(當地時間)2011-07-15 20:43
我想這個日期2011-08 -01 00:00和23:59 2011-08-31
日期1:@"(?<=FROM)\d{4}-\d{2}-\d{2} \d{2}:\d{2}"
日期2:@"(?<=TO)\d{4}-\d{2}-\d{2} \d{2}:\d{2}"
這應該做到這一點:
var dateRegex = @"\d{4}-\d{2}-\d{2} \d{2}:\d{2}";
var regex = new Regex(@"FROM (" + dateRegex + @") TO (" + dateRegex + @") \(Local Time\) .*$");
var text = @"FROM 2011-08-01 00:00 TO 2011-08-31 23:59 (Local Time) 2011-07-15 20:43";
var match = regex.Match(text);
if (match.Success)
{
Console.WriteLine("1: " + match.Groups[1].Value.ToString());
Console.WriteLine("2: " + match.Groups[2].Value.ToString());
}
這應該給你你需要什麼(我做這樣的假設字符串將始終以這種格式 - 所以沒有錯誤處理):
string text = "FROM 2011-08-01 00:00 TO 2011-08-31 23:59 (Local Time) 2011-07-15 20:43";
Regex r = new Regex(@"(?<=FROM)(.*?)(?=\()",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
string[] dates = r.Split(text).GetValue(1).ToString().Trim().Split(' ');
string date1 = dates[0].ToString() + " " + dates[1].ToString();
string date2 = dates[3].ToString() + " " + dates[4].ToString();