C # method for judging the correct date format entered by regular expressions,
This article will introduce an example code to explain how to use regular expressions to make C # determine whether the input date format is correct. I hope this code will help you.
Generally, when we use C # To Write System programs or Web development, we will encounter a situation where we need to verify whether the input string is a date, the following describes a very comprehensive method to verify a date using a regular expression:
C Regular Expression Date Code 1:
///
/// Whether it is a date string
///
///
Date string)
///
Public static bool IsDate (string StrSource)
{
Return Regex. IsMatch (StrSource, @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 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-) $ ");
}
///
/// Whether it is a time string
///
///
Time string (15:00:00)
///
Public static bool IsTime (string StrSource)
{
Return Regex. IsMatch (StrSource, @ "^ (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D: [0-5]? \ D) $ ");
}
///
/// Whether it is a date + time string
///
///
///
Public static bool IsDateTime (string StrSource)
{
Return Regex. IsMatch (StrSource, @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 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 2:
// Whether it 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 )(? 'Sept' [/.-]) (? 'Month' 0? [1-9] | 1 [012]) \ 2
(? 'Day '((? (0? [1-9] | 1 [012]) (: [0-5]? \ D) {0, 2} (\ x20 [AP] M) | ([01]?
\ 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 section describes how to use the C # regular expression to determine whether the input date format is correct. It is helpful for you to learn the c Regular Expression date judgment. At the same time, I would like to thank you for your constant support for our website.