手机与电话的正则表达式
验证手机号码的正确性:^0{0,1}13[0-9]{9}$
验证电话号码的正确性:(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)
验证有效 日期时间 的正则表达式(已解决闰二月)
^((\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));
}
}
posted on 2007-05-10 11:26
larryjava 阅读(1018)
评论(0) 编辑 收藏