C # (Regular Expression) common usage of the RegEx class

Source: Internet
Author: User
Tags valid email address
Using system;
Using system. Text. regularexpressions;
Namespace metarcommonsupport
{
///

/// Some special function data check is implemented through the RegEx class in the framwork class library
///
Public class metarnetregex
{

Private Static metarnetregex instance = NULL;
Public static metarnetregex getinstance ()
{
If (metarnetregex. instance = NULL)
{
Metarnetregex. instance = new metarnetregex ();
}
Return metarnetregex. instance;
}
Private metarnetregex ()
{
}
///

/// Judge that the input string contains only Chinese Characters
///
//////
Public static bool ischinesech (string input)
{
RegEx = new RegEx ("^ [\ u4e00-\ u9fa5] + $ ");
Return RegEx. ismatch (input );
}

///

/// Match the phone number of a three-or four-digit area code. The area code can be enclosed in parentheses,
/// Or do not use it. The area code and the local code can be separated by a hyphen or space,
/// There can be no interval
/// \ (0 \ D {2} \) [-]? \ D {8} | 0 \ D {2} [-]? \ D {8} | \ (0 \ D {3} \) [-]? \ D {7} | 0 \ D {3} [-]? \ D {7}
///
//////
Public static bool isphone (string input)
{
String Pattern = "^ \ (0 \ D {2} \) [-]? \ D {8 }$ | ^ 0 \ D {2} [-]? \ D {8 }$ | ^ \ (0 \ D {3} \) [-]? \ D {7 }$ | ^ 0 \ D {3} [-]? \ D {7} $ ";
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}
///

/// Determine whether the entered string is a valid mobile phone number
///
//////
Public static bool ismobilephone (string input)
{
RegEx = new RegEx ("^ 13 \ D {9} $ ");
Return RegEx. ismatch (input );

}

///

/// Judge that the input string contains only numbers
/// Can match integers and floating-point numbers
/// ^ -? \ D + $ | ^ (-? \ D +) (\. \ D + )? $
///
//////
Public static bool isnumber (string input)
{
String Pattern = "^ -? \ D + $ | ^ (-? \ D +) (\. \ D + )? $ ";
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}
///

/// Match a non-negative integer
///
///
//////
Public static bool isnotnagtive (string input)
{
RegEx = new RegEx (@ "^ \ D + $ ");
Return RegEx. ismatch (input );
}
///

/// Match a positive integer
///
//////
Public static bool isuint (string input)
{
RegEx = new RegEx ("^ [0-9] * [1-9] [0-9] * $ ");
Return RegEx. ismatch (input );
}

/// Match non-negative floating point number
///
/// ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

///

/// Determine whether the input string contains English letters
///
//////
Public static bool isenglisch (string input)
{
RegEx = new RegEx ("^ [A-Za-Z] + $ ");
Return RegEx. ismatch (input );
}

///

/// Determine whether the input string is a valid email address
///
//////
Public static bool isemail (string input)
{
String Pattern = @ "^ ([\ W-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ W-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ";
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}

///

/// Determine whether the input string contains only numbers and English letters
///
//////
Public static bool isnumandench (string input)
{
String Pattern = @ "^ [A-Za-z0-9] + $ ";
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}

///

/// Determine whether the input string is a hyperlink
///
//////
Public static bool isurl (string input)
{
// String pattern = @ "http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)? ";
String Pattern = @ "^ [A-Za-Z] +: // (\ W + (-\ W + )*)(\. (\ W + (-\ W + )*))*(\? \ S *)? $ ";
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}

///

/// Determine whether the input string represents an IP address
///
////// True if it is an IP address
Public static bool isipv4 (string input)
{

String [] IPS = input. Split ('.');
RegEx = new RegEx (@ "^ \ D + $ ");
For (INT I = 0; I {
If (! RegEx. ismatch (IPS [I])
{
Return false;
}
If (convert. touint16 (IPS [I])> 255)
{
Return false;
}
}
Return true;
}

///

/// Calculate the character length of a string. A Chinese character is counted as two characters.
///
////// Returns the length of a string.
Public static int getcount (string input)
{
Return RegEx. Replace (input, @ "[\ u4e00-\ u9fa5/g]", "AA"). length;
}

///

/// Call the ismatch function in RegEx to implement regular expression matching.
///
///////// True if the regular expression finds a match. Otherwise, false.
Public static bool ismatch (string pattern, string input)
{
RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}

///

/// Starting from the first character in the input string, replace the string with all matching items in the specified regular expression pattern.
///
//////////// Returns the result after replacement.
Public static string Replace (string pattern, string input, string replacement)
{
RegEx = new RegEx (pattern );
Return RegEx. Replace (input, replacement );
}

///

/// Split the input string at the position defined by the regular expression mode.
///
/////////
Public static string [] Split (string pattern, string input)
{
RegEx = new RegEx (pattern );
Return RegEx. Split (input );
}
///

/// Determine whether the entered string is a valid IPv6 address
///
//////
Public static bool isipv6 (string input)
{
String Pattern = "";
String temp = input;
String [] STRs = temp. Split (':');
If (STRs. length> 8)
{
Return false;
}
Int COUNT = metarnetregex. getstringcount (input ,"::");
If (count> 1)
{
Return false;
}
Else if (COUNT = 0)
{
Pattern = @ "^ ([\ da-F] {1, 4} :) {7} [\ da-F] {1, 4} $ ";

RegEx = new RegEx (pattern );
Return RegEx. ismatch (input );
}
Else
{
Pattern = @ "^ ([\ da-F }:: ([\ da-F] {} :) {} [\ da-F] {} $ ";
RegEx regex1 = new RegEx (pattern );
Return regex1.ismatch (input );
}

}
/*************************************** *****************************
* 1. Use ":" to split the string and check whether the length of the string array is less than or equal to 8.
* 2. Check whether the entered IPv6 string contains ":".
* 3. If ":" is not found, use ^ ([\ da-F] {} :) {7} [\ da-F] {} $.
* 4. If ":" exists, Judge ":" whether or not it appears again
* 5. If more than one occurrence occurs, false is returned.
* 6. ^ ([\ da-F }:: ([\ da-F] {} :) {} [\ da-F] {} $
**************************************** ***************************/
///

/// Determine the number of times the Compare string appears in the input string
///
///////// Number of times the string compare appears in the input string
Private Static int getstringcount (string input, string compare)
{
Int Index = input. indexof (compare );
If (index! =-1)
{
Return 1 + getstringcount (input. substring (index + compare. length), compare );
}
Else
{
Return 0;
}

}
}
}

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.