A summary of several C # common regular expressions-basic application

Source: Internet
Author: User
Using System;
Using System.Text.RegularExpressions;

Namespace Commontools
{
/**////<summary>
Summary description of the regexlib.
</summary>
public class Regexlib
{

Verify email address
public static bool IsValidEmail (string strin)
{
Return true if Strin are in valid e-mail format.
Return Regex.IsMatch (Strin, @ "^" ^ [\w-\.] +) @ (\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | (([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $");
}
The Dd-mm-yy date form replaces the Mm/dd/yy date form.
public static string Mdytodmy (String input)
{
return Regex.Replace (Input, "\\b (? \\d{1,2})/(? \\d{1,2})/(? \\d{2,4}) \\b", "${day}-${month}-${year}");
}
Verify that you are a decimal
public static bool Isvaliddecimal (string strin)
{
return Regex.IsMatch (strin,@ "[0].\d{1,2}|[ 1] ");
}
Verify that you are a phone number
public static bool Isvalidtel (string strin)
{
return Regex.IsMatch (strin,@) (\d+-)? ( \d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8}) (-\d+)? ");
}
Date verified
public static bool Isvaliddate (string strin)
{
return Regex.IsMatch (strin,@ "^2\d{3}-" (?: 0?) [1-9]|1[0-2])-(?: 0? [1-9]| [1-2]\d|3[0-1]) (?: 0? [1-9]|1\d|2[0-3]):(?: 0? [1-9]| [1-5]\d):(?: 0? [1-9]| [1-5]\d) $ ");
}
Verify suffix Name
public static bool Isvalidpostfix (string strin)
{
return Regex.IsMatch (strin,@ "\. i:gif|jpg) $ ");
}
Verify that the characters are between 4 and 12
public static bool Isvalidbyte (string strin)
{
return Regex.IsMatch (strin,@ "^[a-z]{4,12}$");
}
Verifying IP
public static bool Isvalidip (string strin)
{
return Regex.IsMatch (strin,@ "^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. ( \D{1,2}|1\D\D|2[0-4]\D|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $ ");
}

}
}

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.