Unfamiliar with regular expressions, record several expressions that have been verified elsewhere, date, number, email, and URL Verification

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.