我怎样才能在这个字符串中得到这2个日期?
FROM 2011-08-01 00:00至2011-08-31 23:59(当地时间)2011-07-15 20时43分
我希望这个日期2011-08-01 00:00和2011-08-31 23:59
答案 0 :(得分:2)
日期1:@"(?<=FROM )\d{4}-\d{2}-\d{2} \d{2}:\d{2}"
日期2:@"(?<=TO )\d{4}-\d{2}-\d{2} \d{2}:\d{2}"
答案 1 :(得分:1)
应该这样做:
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());
}
答案 2 :(得分:1)
这应该给你你需要的东西(我假设字符串将始终采用这种格式 - 所以没有错误处理):
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();