This article will introduce an example code to explain how a regular expression is used to make C # determine whether the input date format is correct. I hope this code will be helpful to everyone.
Usually when we write a system program or Web development in C #, we encounter situations where we need to verify that the input string is a date, here's a very comprehensive way to validate a date with a regular expression:
C Regular Expression date code one:
<summary>///is a date-type string///</summary>///<param name= "strsource" > Date string (2008-05-08) ;/param>///<returns></returns> public static bool IsDate (string strsource) {return Reg Ex. IsMatch (strsource, @ "^ (1[6-9]|[ 2-9]\d) \d{2})-(0?[ 13578]|1[02])-(0?[ 1-9]| [12]\d|3[01]) | (((1[6-9]| [2-9]\d) \d{2})-(0?[ 13456789]|1[012])-(0?[ 1-9]| [12]\d|30)] | (((1[6-9]| [2-9]\d) \d{2}) -0?2-(0?[ 1-9]|1\D|2[0-9]) | (((1[6-9]| [2-9]\d) (0[48]|[ 2468][048]| [13579] [26]) | ((16| [2468] [048]| [3579] [26])
00)) ( -0?2-29-)); }///<summary>///is a time-type string///</summary>///<param name= "source" > Time string (15:00:00) ;/param>///<returns></returns> public static bool Istime (string strsource) {return Reg Ex. IsMatch (strsource, @ "^" ^ (20|21|22|23|[
0-1]?\d): [0-5]?\d:[0-5]?\d] $ ");
///<summary>///is a date + time-type string///</summary>///<param name= "source" ></param> <returns></returns> public static bool Isdatetime (string strsource) {return REGEX.ISMATC H (strsource, @ "^" ((1[6-9]|[ 2-9]\d) \d{2})-(0?[ 13578]|1[02])-(0?[ 1-9]| [12]\d|3[01]) | (((1[6-9]| [2-9]\d) \d{2})-(0?[ 13456789]|1[012])-(0?[ 1-9]| [12]\d|30)] | (((1[6-9]| [2-9]\d) \d{2}) -0?2-(0?[ 1-9]|1\D|2[0-8]) | (((1[6-9]| [2-9]\d) (0[48]|[ 2468][048]| [13579] [26]) | ((16| [2468] [048]| [3579] [26]) 00)) ( -0?2-29-)) (20|21|22|23|[
0-1]?\d): [0-5]?\d:[0-5]?\d] $ "); }
C Regular Expression date code two:
is an integer public
static bool Isint (string strsource)
{return
Regex.IsMatch (strsource, @ "^[0-9]*$");
public static bool IsDate (string strdate)
{
if (string). IsNullOrEmpty (strdate)) return
false;
String S_reg = @ "^" Ni: (? =\\d) (? Year ' ((1[6-9]) | ( [2-9]\\d)) \\d\\d) (? ' Sep ' [/.-]) (? Month ' 0? [1-9]|1[012]) \\2
(? ') Day ' ((? <! ( \\2 ((0?[ 2469]) |11) \\2) 31 | (? <!\\2 (0?2) \\2) (29|30) | ((? <= (1[6-9]|[ 2-9]\\d) (0[48]|
[2468] [048]| [13579] [26]) | (16| [2468] [048]| [3579] [26]) 00) \\2\\3\\2) 29 | ((0?) [1-9]) | (1\\d) | (2[0-
8])) (?:(? =\\x20\\d) \\x20|$)? ((?<time> (0?[ 1-9]|1[012]) (: [0-5]?\\d) {0,2} (\\x20[ap]m) | ([a]?
\\d|2[0-3]) (: [0-5]?\\d) {1,2}))? $";
Regex reg = new regex (S_reg);
if (Reg. IsMatch (Strdate.tolower ()) return
true;
else return
false;
The above is a small series to introduce the C # Regular expression to determine whether the input date format is correct all the content, I hope that you learn C regular expression of the date of judgment help. At the same time, thank you very much for your support of cloud-dwelling community.