验证有效 "日期时间" 的正则表达式(已解决闰二月)
^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$
C# Code Test:
public class Class1
{
static void Main(string[] args)
{
// string regex = @"^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$";
string regex = @"^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578]
)|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4
69])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///
s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([1
3579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((
0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((
0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9]
)|(2[0-8]))))))"; //日期部分
regex += @"(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$"; //时间部分
System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Multiline) | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
string s = "2003-02-29 23:59:59";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
s = "2004-02-29 23:59:59";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
s = "2004-04-30 0:59:59";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
s = "2004-04-30 01:11:0";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
s = "2004-04-30 0:0:0";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
s = "2004-04-30 00:00:00";
System.Console.Out.WriteLine(s + " {0}",reg.IsMatch(s));
}
}
//Java Code Test
import java.util.regex.*;
class Class1
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("^((//d{2}(([02468][048])|([13579][26]))[//-/////s]?((((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[//-/////s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?((0?[1-9])|([1-2][0-9])))))|(//d{2}(([02468][1235679])|([13579][01345789]))[//-/////s]?((((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[//-/////s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(//s(((0?[0-9])|([1-2][0-3]))//:([0-5]?[0-9])((//s)|(//:([0-5]?[0-9])))))?$");
String s = "2003-02-29 23:59:59";
System.out.println(s + " " + p.matcher(s).matches());
s = "2004-02-29 23:59:59";
System.out.println(s + " " + p.matcher(s).matches());
s = "2004-04-31 0:59:59";
System.out.println(s + " " + p.matcher(s).matches());
s = "2004-04-30 01:11:0";
System.out.println(s + " " + p.matcher(s).matches());
s = "2004-04-30 0:0:0";
System.out.println(s + " " + p.matcher(s).matches());
s = "2004-04-30 00:00:59";
System.out.println(s + " " + p.matcher(s).matches());
}
}