C # Regex detail (Turn)

Source: Internet
Author: User
Tags array length valid email address

Using System;
Using System.Text.RegularExpressions;
Namespace Metarcommonsupport
{
<summary>
Through the Regex class in the Framwork class library, some special function data checking is implemented.
</summary>
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 ()
{
}
<summary>
To determine that the input string contains only Chinese characters
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Ischinesech (string input)
{
Regex regex = new Regex ("^[/u4e00-/u9fa5]+$");
return regex. IsMatch (input);
}

<summary>
A phone number that matches a 3-bit or 4-bit area code, where the area code can be enclosed in parentheses,
You can also use a hyphen or space between the area code and the local number,
You can also have no interval
/(0/d{2}/) [-]?/d{8}|0/d{2}[-]?/d{8}|/(0/d{3}/) [-]?/d{7}|0/d{3}[-]?/d{7}
</summary>
<param name= "Input" ></param>
<returns></returns>
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 regex = new regex (pattern);
return regex. IsMatch (input);
}
<summary>
Determines whether the input string is a legitimate cell phone number
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Ismobilephone (string input)
{
Regex regex = new Regex ("^13//d{9}$");
return regex. IsMatch (input);

}


<summary>
To determine that the input string contains only numbers
can match integers and floating-point numbers
^-?/d+$|^ (-?/d+) (/./d+)? $
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Isnumber (string input)
{
String pattern = "^-?//d+$|^ (-?//d+) (
.//d+)? $ ";
Regex regex = new regex (pattern);
return regex. IsMatch (input);
}
<summary>
matching nonnegative integers
///
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Isnotnagtive (string input)
{
Regex regex = new Regex (@ "^/d+$");
return regex. IsMatch (input);
}
<summary>
Match positive integer
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Isuint (string input)
{
Regex regex = new Regex ("^[0-9]*[1-9][0-9]*$");
return regex. IsMatch (input);
}
<summary>
Determines that the string word entered contains an English letter
</summary>
<param name= "Input" ></param>
<returns></returns>
public static bool Isenglisch (string input)
{
Regex regex = new Regex ("^[a-za-z]+$");
return regex. IsMatch (input);
}


<summary>
Determine if the input string is a valid email address
</summary>
<param name= "Input" ></param>
<returns></returns>
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 regex = new regex (pattern);
return regex. IsMatch (input);
}


  ///<summary>
  ///Determines whether the input string contains only numbers and English letters
  ///</summary
  ///<param name= "input" ></PARAM>
  ///<returns></returns
   public static bool Isnumandench (string input)
   {
    string Patt Ern = @ "^[a-za-z0-9]+$";
    Regex regex = new regex (pattern);
    return regex. IsMatch (input);
  }


  ///<summary>
  ///to determine if the string entered is a hyperlink
  ///</summary>
  ///<param name= "input" ></PARAM>
  ///<returns></returns>
   public static bool Isurl (string input)
   {
   //string pattern = @ "H ttp://([/w-]+/.) +[/w-]+ (/[/w-/?%&=]*)? ";
    String pattern = @ "^[a-za-z]+://(/w+ (-/w+) *) (/.) ( /w+ (-/w+) *)) * (/?/s*) $ ";
    Regex regex = new regex (pattern);
    return regex. IsMatch (input);
  }


  ///<summary>
  ///Determines whether the input string represents an IP address
  ///</summary
  ///<param name= "input" > compared string </param>
  ///<returns> is the IP address true</returns>
   public static bool IsIPv4 (string input)
   {
  &NBSP;&NBSP
    string[] IPs = input. Split ('. ');
    Regex regex = new Regex (@ "^/d+$");
    for (int i = 0; i<ips.length; i++)
    {
     if!re Gex. IsMatch (Ips[i])
     {
      return false;
     }
     if (convert.touint16 (ips[i]) > 255)
     {
& nbsp;     return false;
    }
   }
    return true;
  }


<summary>
Calculates the character length of a string, and a Chinese character is evaluated to two characters
</summary>
<param name= "Input" > Strings to be computed </param>
<returns> returns the length of the string </returns>
public static int GetCount (string input)
{
Return Regex.Replace (input,@ "[/u4e00-/u9fa5/g]", "AA"). Length;
}

<summary>
Call the IsMatch function in the regex to implement a generic regular expression match
</summary>
<param name= "pattern" > Regular expression Patterns to match. </param>
<param name= "Input" > Strings to search for matches </param>
<returns> true if the regular expression finds a match, otherwise, false. </returns>
public static bool IsMatch (string pattern, string input)
{
Regex regex = new regex (pattern);
return regex. IsMatch (input);
}

<summary>
Replaces all occurrences of the specified regular expression pattern with a replacement string, starting with the first character in the input string.
</summary>
<param name= "pattern" > Mode string </param>
<param name= "Input" > Input string </param>
<param name= "Replacement" > string used for substitution </param>
<returns> returns the result after being replaced </returns>
public static string Replace (string pattern, string input, string replacement)
{
Regex regex = new regex (pattern);
return regex. Replace (input,replacement);
}

<summary>
Splits the input string at the location defined by the regular expression pattern.
</summary>
<param name= "pattern" > Mode string </param>
<param name= "Input" > Input string </param>
<returns></returns>
public static string[] Split (string pattern, string input)
{
Regex regex = new regex (pattern);
return regex. Split (input);
}
<summary>
Determines whether the input string is a valid IPV6 address
</summary>
<param name= "Input" ></param>
<returns></returns>
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 regex = new regex (pattern);
return regex. IsMatch (input);
}
Else
{
Pattern = @ "^ ([/da-f]{1,4}:) {0,5}::([/da-f]{1,4}:) {0,5}[/da-f]{1,4}$";
Regex regex1 = new regex (pattern);
Return REGEX1. IsMatch (input);
}

}
/* *******************************************************************
* 1, by ":" to split the string to see whether the string array length is less than or equal to 8
* 2, to determine whether the input IPV6 string in the "::".
* 3, if not "::" Using ^ ([/da-f]{1,4}:) {7}[/da-f]{1,4}$ to judge
* 4, if there is "::", Judge "::" Whether to stop appearing once
* 5, if there is more than one return false
* 6, ^ ([/da-f]{1,4}:) {0,5}::([/da-f]{1,4}:) {0,5}[/da-f]{1,4}$
* ******************************************************************/
<summary>
Determines the number of times a string compare appears in the input string
</summary>
<param name= "Input" > Source string </param>
<param name= "Compare" > string for comparison </param>
<returns> string Compare the number of occurrences in the input string </returns>
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;
}

}
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.