29 September 2011

checking regular expression for different date formats

string spl = @"([/|\-|.|\s])?";
string d = "(0?[1-9]|[12][0-9]|3[01])";
string m = "(0?[1-9]|1[012])";
string mmm = "([J|j]an(uary)?|[F|f]eb(ruary)?|([M|m]a(r(ch)?|y))|[A|a]pr(il)?|[J|j]u(n(e)?|l(y)?)|[A|a]ug(ust)?|[O|o]ct(ober)?|([S|s]ep(t)?|[N|n]ov|[D|d]ec)(ember)?)";
string y = "((19|20)?[0-9]{2})";

string timspl = @"([:|.|\s])?";
string hh = @"(0?[0-9]|1[012])";
string min = @"([0?|1|2|3|4|5][0-9]|60)";
string ss = @"([0?|1|2|3|4|5][0-9]|60)";
string tt = @"([A|a|p|P]m|[A|P]M)?";
string time = @"(" + hh + timspl + min + timspl + ss + ")?"+spl+tt;

string zzz = @"(\+[0-9]{2}(\:)?[0-9]{2})?"+spl+tt;

string date = @"("+d+spl+"("+m+"|"+mmm+")|("+m+"|"+mmm+"+)+"+spl+d+")"+spl+y+"";

string exp = "^(" + date + spl + time + spl + zzz + ")|(" + time + spl + date + spl + zzz + ")|(" + zzz + spl + time + spl + date + ")$";
if (Regex.IsMatch(txtreg.Text.ToLower().Trim(), exp))
lblreg.Text = "available";
else
lblreg.Text = "not available";

No comments: