Domanda

How can I do the following in C# :

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

Soluzione 2

The below code should get you where you want to be.

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
}

Altri suggerimenti

You can use the Regex.IsMatch instead (docs).

Regex.IsMatch("2013/03/05 15:22:00", @"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$"); // true if match
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top