2013-03-05 129 views
0

我怎樣才能做到在C#中的以下內容:的Javascript regexp.test().NET相當於

var re = /^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$/; 
re.test('2013/03/05 15:22:00'); // returns true 
+5

你還沒有問一個問題......不過,你可能想看看[正則表達式(http://msdn.microsoft.com/en-gb/library/system.text.regularexpressions。 regex.aspx)類。話雖如此,你可能會更好看看[DateTime.TryParse](http://msdn.microsoft.com/en-GB/library/system.datetime.tryparse.aspx)方法。 – 2013-03-05 14:26:46

+1

OBTW,沒有_exact_等價物,因爲與JavaScript不同,C#不支持正則表達式。該支持在.NET Framework中。 – 2013-03-05 14:28:22

+0

你想讓我做什麼?只是重新標題? – leaf 2013-03-05 14:53:49

回答

3

下面的代碼應該可以讓你知道你想要的位置。

Regex rx = new Regex(@"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$"); 
String test = "2013/03/05 15:22:00"; 

if (rx.IsMatch(test)) 
{ 
    //Test String matches 
} 
else 
{ 
    //Test String does not match 
} 
+0

哎呀..看起來像達文在我打字時拿到了它。 – Geneb 2013-03-05 14:31:26

6

可以使用Regex.IsMatch代替(docs)。

Regex.IsMatch("2013/03/05 15:22:00", @"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$"); // true if match 
+0

@KristerRenaud謝謝,我不確定開始正斜槓! – 2013-03-05 15:15:15