Unfamiliar with regular expressions, record several expressions that have been verified elsewhere, date, number, email, and URL Verification
Last Update:2018-12-07
Source: Internet
Author: User
/**/ /// <Summary>
/// Determine whether the regular expression is a number
/// </Summary>
/// <Param name = "strnumber"> </param>
/// <Returns> </returns>
Public Static Bool Isnumber ( String Strnumber)
{
// ^ [-+]? [1-9] \ D *\.? [0] * $
RegEx objnotnumberpattern = New RegEx ( " [^ 0-9.-] " );
RegEx objtwodotpattern = New RegEx ( " [0-9] * [.] [0-9] * [.] [0-9] * " );
RegEx objtwominuspattern = New RegEx ( " [0-9] * [-] [0-9] * [-] [0-9] * " );
String strvalidrealpattern = " ^ ([-] | [.] | [-.] | [0-9]) [0-9] * [.] * [0-9] + $ " ;
String strvalidintegerpattern = " ^ ([-] | [0-9]) [0-9] * $ " ;
RegEx objnumberpattern = New RegEx ( " ( " + Strvalidrealpattern + " ) | ( " + Strvalidintegerpattern + " ) " );
Return ! Objnotnumberpattern. ismatch (strnumber) &&
! Objtwodotpattern. ismatch (strnumber) &&
! Objtwominuspattern. ismatch (strnumber) &&
Objnumberpattern. ismatch (strnumber );
}
/**/ /// <Summary>
/// Regular Expression Judge whether it is a date
/// Verification format:
/// YYYY-MM
/// YYYY-MM (M)-dd (m)
/// Yyyymmdd
/// </Summary>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Public Static Bool Isdate ( String Date)
{
// ([0-9] {4}-) ([0-9] {1, 2}-) [0-9] {1, 2 })
RegEx ympattern = New RegEx ( @" ^ \ D {4 }-? (? : 0 [1-9] | 1 [0-2]) $ " );
RegEx yyyymmdd = New 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]) " ); // Date
Return ! Ympattern. ismatch (date) &&
Yyyymmdd. ismatch (date );
}
/**/ /// <Summary>
/// Regular Expression Judge whether it is a date
/// Verification format:
/// YYYY-MM (M)-dd (m) hh: mm
/// Yyyymmdd hh: mm
/// </Summary>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Public Static Bool Isdatetime ( String Date)
{
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]) " ; // Date
RegEx + = @" (\ S (0? [0-9]) | ([1-2] [0-3]) \ :( [0-5]? [0-9]) (\ s) | (\ :( [0-5]? [0-9])? $ " ; // Time part
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 );
Return Reg. ismatch (date );
}
Public Static Bool Isemail ( String Mail)
{
RegEx Reg= NewRegEx (@"^ ([A-zA-Z0-9 _ \-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([a-zA-Z0-9 \-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $");
ReturnReg. ismatch (Mail );
}
/**/ /// <Summary>
/// Whether the regular expression is a URL, Verification format:
/// Wwww. XXX. XXX
/// XXX. XXX. XXX
/// Http://www.xxx.xxx
/// Http://xxx.xxx.xxx
/// </Summary>
/// <Param name = "Uri"> </param>
/// <Returns> </returns>
Public Static Bool Isuri ( String Uri)
{< br> RegEx Reg = New RegEx ( @" ^ (HT | f) TP (S ?)) \://)? ([A-zA-Z0-9 _ \-] {2,} \.) + [A-Za-Z] {2,}) | ((? :(?: 25 [0-5] | 2 [0-4] \ d | [01] \ D \ d | \ D? \ D )(? (\.? \ D) \.) {4} (: [a-zA-Z0-9] + )? (/[A-zA-Z0-9 \-\._\? \, \ '/\ + & Amp; % \ $ # \ = ~] *)? $ " );
return Reg. ismatch (URI);
}