C# Check Help Class - Regular Expressions

Keywords: C# Mobile Mac encoding ftp

I. Brief Introduction

Many times we need to use some validation methods. Sometimes when we need to use regular expressions to validate data, we often need to go to the Internet for a long time, and the results are not quite what we want. So I share my checking help class, share it here, leave a bottom for myself, and also give you a reference for friends who need it.

II. Check class fragments

2.1 Mailbox Verification

#Region Is Email
/// <summary>
/// Is it mailbox?
/// </summary>
/// <param name="value">mailbox address </param>
/// Whether < param name= "isRestrict"> is validated in strict mode </param>
/// <returns></returns>
public static bool IsEmail(string value, bool isRestrict=false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern = isRestrict
        ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
        
    return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}

/// <summary>
/// Is there a mailbox?
/// </summary>
/// <param name="value">value</param>
/// Whether < param name= "isRestrict"> is validated in strict mode </param>
/// <returns></returns>
public static bool HasEmail(string value, bool isRestrict = false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern = isRestrict
        ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
        : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
    return value.IsMatch(pattern, RegexOptions.IgnoreCase);
}
#endregion

2.2 Mobile Phone Number Verification

#Region IsPhoneNumber
/// <summary>
/// Legitimate mobile phone number
/// </summary>
/// <param name="value">mobile phone number </param>
/// <returns></returns>
public static bool IsPhoneNumber(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$");
}
#endregion

#Region Is Mobile Number
/// <summary>
/// Is it a cell phone number?
/// </summary>
/// <param name="value">mobile phone number </param>
/// Whether < param name= "isRestrict"> is validated in strict mode </param>
/// <returns></returns>
public static bool IsMobileNumberSimple(string value, bool isRestrict = false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern=isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$";
    return value.IsMatch(pattern);
}
/// <summary>
/// Is it a cell phone number?
/// </summary>
/// <param name="value">mobile phone number </param>
/// <returns></returns>
public static bool IsMobileNumber(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    value = value.Trim().Replace("^", "").Replace("$", "");
    /**
     * Phone number: 
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
     * Mobile number: 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705
     * Unicom Section: 130, 131, 132, 155, 156, 185, 186, 145, 176, 1709
     * Electrical signal segment: 133, 153, 180, 181, 189, 177, 1700
     */
    return value.IsMatch(@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$");
}

/// <summary>
/// Is there a mobile phone number?
/// </summary>
/// <param name="value">value</param>
/// Whether < param name= "isRestrict"> is validated in strict mode </param>
/// <returns></returns>
public static bool HasMobileNumberSimple(string value, bool isRestrict = false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string pattern = isRestrict ? @"[1][3-8]\d{9}" : @"[1]\d{10}";
    return value.IsMatch(pattern);
}
#endregion

#Region Is China Mobile Phone (whether China Mobile Number or not)
/// <summary>
/// Is China Mobile Number
/// </summary>
/// <param name="value">mobile phone number </param>
/// <returns></returns>
public static bool IsChinaMobilePhone(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    /**
     * China Mobile: China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     */
    return value.IsMatch(@"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)");
}
#endregion

#Region Is China Unicom Phone (whether China Unicom number or not)
/// <summary>
/// Is China Unicom Number
/// </summary>
/// <param name="value">mobile phone number </param>
/// <returns></returns>
public static bool IsChinaUnicomPhone(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    /**
     * China Unicom
     * 130,131,132,155,156,185,186,145,176,1709
     */
    return value.IsMatch(@"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)");
}
#endregion

#Region Is China Telecom Phone (whether China Telecom Number or not)
/// <summary>
/// Is China Telecom Number
/// </summary>
/// <param name="value">mobile phone number </param>
/// <returns></returns>
public static bool IsChinaTelecomPhone(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    /**
     * China Telecom: China Telecom
     * 133,153,180,181,189,177,1700
     */
    return value.IsMatch(@"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)");
}
#endregion

2.3 Identity Card Verification

#Region IsIdCard (ID number or not)
/// <summary>
/// Is there an ID number?
/// </summary>
/// <param name="value">ID card </param>
/// <returns></returns>
public static bool IsIdCard(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    if (value.Length == 15)
    {
        return value.IsMatch(@"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
    }
    return value.Length == 0x12 &&
           value.IsMatch(@"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$",
               RegexOptions.IgnoreCase);
}
#endregion

2.4 Base64 Coding Verification

#Region IsBase64String (whether or not Base64 encoding)
/// <summary>
/// Whether Base64 encoding
/// </summary>
/// <param name="value">Base64 string </param>
/// <returns></returns>
public static bool IsBase64String(string value)
{
    return value.IsMatch(@"[A-Za-z0-9\+\/\=]");
}
#endregion

2.5 Time Validation

#Region IsDate (Date or not)
/// <summary>
/// Is it a date?
/// </summary>
/// <param name="value">date string </param>
/// <param name="isRegex"> whether regular validation </param>
/// <returns></returns>
public static bool IsDate(string value,bool isRegex=false)
{
    if (value.IsEmpty())
    {
        return false;
    }
    if (isRegex)
    {
        //Considering the 366 days of the quadrennial year, there is a special February date.
        return
            value.IsMatch(
                @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
    }
    DateTime minValue;
    return DateTime.TryParse(value, out minValue);
}

/// <summary>
/// Is it a date?
/// </summary>
/// <param name="value">date string </param>
/// <param name="format">formatted string </param>
/// <returns></returns>
public static bool IsDate(string value, string format)
{
    return IsDate(value, format, null, DateTimeStyles.None);
}

/// <summary>
/// Is it a date?
/// </summary>
/// <param name="value">date string </param>
/// <param name="format">formatted string </param>
/// <param name="provider">formatting provider</param>
/// <param name="styles">date format </param>
/// <returns></returns>
public static bool IsDate(string value, string format, IFormatProvider provider, DateTimeStyles styles)
{
    if (value.IsEmpty())
    {
        return false;
    }
    DateTime minValue;
    return DateTime.TryParseExact(value, format, provider, styles, out minValue);
}
#endregion

#Region IsDateTime (valid time)
/// <summary>
/// Is it greater than the minimum time?
/// </summary>
/// <param name="value">time </param>
/// <param name="min">minimum time </param>
/// <returns></returns>
public static bool IsDateTimeMin(string value, DateTime min)
{
    if (value.IsEmpty())
    {
        return false;
    }
    DateTime dateTime;
    if (DateTime.TryParse(value, out dateTime))
    {
        if (DateTime.Compare(dateTime, min) >= 0)
        {
            return true;
        }
    }
    return false;
}
/// <summary>
/// Is it less than the maximum time?
/// </summary>
/// <param name="value">time </param>
/// <param name="max">maximum time </param>
/// <returns></returns>
public static bool IsDateTimeMax(string value, DateTime max)
{
    if (value.IsEmpty())
    {
        return false;
    }
    DateTime dateTime;
    if (DateTime.TryParse(value, out dateTime))
    {
        if (DateTime.Compare(max, dateTime) >= 0)
        {
            return true;
        }
    }
    return false;
}
#endregion

2.6 Url Verification

#Region IsUrl (whether or not Url address)
/// <summary>
/// Whether or not Url address (Unified Resource Location)
/// </summary>
/// <param name="value">url address </param>
/// <returns></returns>
public static bool IsUrl(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return
        value.IsMatch(
            @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$",
            RegexOptions.IgnoreCase);
}
#endregion

#Region IsUri (Uri or not)
/// <summary>
/// Whether Uri (Unified Resource Identification)
/// </summary>
/// <param name="value">uri</param>
/// <returns></returns>
public static bool IsUri(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    if (value.IndexOf(".", StringComparison.OrdinalIgnoreCase) == -1)
    {
        return false;
    }
    var schemes = new[]
    {
        "file",
        "ftp",
        "gopher",
        "http",
        "https",
        "ldap",
        "mailto",
        "net.pipe",
        "net.tcp",
        "news",
        "nntp",
        "telnet",
        "uuid"
    };

    bool hasValidSchema = false;
    foreach (string scheme in schemes)
    {
        if (hasValidSchema)
        {
            continue;
        }
        if (value.StartsWith(scheme, StringComparison.OrdinalIgnoreCase))
        {
            hasValidSchema = true;
        }
    }
    if (!hasValidSchema)
    {
        value = "http://" + value;
    }
    return Uri.IsWellFormedUriString(value, UriKind.Absolute);
}
#endregion

#Region IsMainDomain
/// <summary>
/// Does the main domain name or the domain name at the beginning of the www
/// </summary>
/// <param name="value">url address </param>
/// <returns></returns>
public static bool IsMainDomain(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return
        value.IsMatch(
            @"^http(s)?\://((www.)?[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
#endregion

2.7 Basic Format Verification

#Regional IsGuid
/// <summary>
/// Is Guid or not?
/// </summary>
/// <param name="guid">Guid string </param>
/// <returns></returns>
public static bool IsGuid(string guid)
{
    if (guid.IsEmpty())
    {
        return false;
    }
    return guid.IsMatch(@"[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
}
#endregion

#Region IsPositiveInteger (positive integer greater than 0)
/// <summary>
/// Positive integers greater than 0
/// </summary>
/// <param name="value">positive integer </param>
/// <returns></returns>
public static bool IsPositiveInteger(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^[1-9]+\d*$");
}
#endregion

#Region IsInt32 (whether Int32 type or not)
/// <summary>
/// Is Int32 Type
/// </summary>
/// <param name="value">integer </param>
/// <returns></returns>
public static bool IsInt32(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^[0-9]*$");
}
#endregion

#Region IsDouble (Double type, if with. Default 1 bit 0)
/// <summary>
/// Double type or not
/// </summary>
/// <param name="value">decimal </param>
/// <returns></returns>
public static bool IsDouble(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^\d[.]?\d?$");
}
/// <summary>
/// Double type or not
/// </summary>
/// <param name="value">decimal </param>
/// <param name="minValue">minimum </param>
/// <param name="maxValue">maximum </param>
/// <param name="digit">decimal number, if 0, no detection </param>
/// <returns></returns>
public static bool IsDouble(string value, double minValue, double maxValue, int digit)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string patten = string.Format(@"^\d[.]?\d{0}$", "{0,10}");
    if (digit > 0)
    {
        patten = string.Format(@"^\d[.]?\d{0}$", "{" + digit + "}");
    }
    if (value.IsMatch(patten))
    {
        double val = Convert.ToDouble(value);
        if (val >= minValue && val <= maxValue)
        {
            return true;
        }
    }
    return false;
}
#endregion

#Region IsInteger (Integer or not)
/// <summary>
/// Is it an integer?
/// </summary>
/// <param name="value">value</param>
/// < returns > results </returns >
public static bool IsInteger(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^\-?[0-9]+$");
}
#endregion

#Region IsUnicode (whether or not Unicode strings)
/// <summary>
/// Is Unicode String or not?
/// </summary>
/// <param name="value">unicode string </param>
/// < returns > results </returns >
public static bool IsUnicode(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return
        value.IsMatch(
            @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$");
}
#endregion

#Regional IsDecimal
/// <summary>
/// Is it digital?
/// </summary>
/// <param name="value">number </param>
/// <returns></returns>
public static bool IsDecimal(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^([0-9])[0-9]*(\.\w*)?$");
}
#endregion

2.8 IP and Mac Address Verification

#Region IsMac (Mac address or not)
/// <summary>
/// Whether Mac address or not
/// </summary>
/// <param name="value">Mac address</param>
/// <returns></returns>
public static bool IsMac(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^([0-9A-F]{2}-){5}[0-9A-F]{2}$") || value.IsMatch(@"^[0-9A-F]{12}$");
}
#endregion

#Region IsIpAddress (whether IP address or not)
/// <summary>
/// Is it an IP address?
/// </summary>
/// <param name="value">ip address</param>
/// < returns > results </returns >
public static bool IsIpAddress(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$");
}
#endregion

2.9 String Verification

#Region IsVersion (valid version number)
/// <summary>
/// Exemplary: 1.3, 1.1.5, 1.25.256
/// </summary>
/// <param name="value">version number </param>
/// <param name="length">length</param>
/// <returns></returns>
public static bool IsVersion(string value, int length = 5)
{
    if (value.IsEmpty())
    {
        return false;
    }
    value = value.Replace("^", "").Replace("$", "");
    return value.IsMatch(string.Format(@"^{0}{1}{2}$", @"\d{0,4}\.(\d{1,4}\.){0,", length, @"}\d{1,4}"));
}
#endregion

#Region IsContains Chinese (Does it contain Chinese)
/// <summary>
/// Is it Chinese?
/// </summary>
/// <param name="value">Chinese </param>
/// <returns></returns>
public static bool IsChinese(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
}
/// <summary>
/// Does it contain Chinese?
/// </summary>
/// <param name="value">Chinese </param>
/// <returns></returns>
public static bool IsContainsChinese(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"[\u4e00-\u9fa5]+",RegexOptions.IgnoreCase);
}
#endregion

#Region IsContains Number
/// <summary>
/// Does it contain numbers?
/// </summary>
/// <param name="value">number </param>
/// <returns></returns>
public static bool IsContainsNumber(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"[0-9]+");
}
#endregion
#Region IsLengthStr (is the string length within the specified range)
/// <summary>
/// Whether the length of the string is within the specified range, one Chinese character is 2 characters
/// </summary>
/// <param name="value">string </param>
/// <param name="begin">start</param>
/// <param name="end">end</param>
/// <returns></returns>
public static bool IsLengthStr(string value, int begin, int end)
{
    int length = Regex.Replace(value, @"[^\x00-\xff]", "OK").Length;
    if ((length <= begin) && (length >= end))
    {
        return false;
    }
    return true;
}
#endregion

#Areas IsNormalChar (normal characters, combinations of letters, numbers, underscores)
/// <summary>
/// Is it a normal combination of characters, letters, numbers and underscores?
/// </summary>
/// <param name="value">string </param>
/// <returns></returns>
public static bool IsNormalChar(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"[\w\d_]+", RegexOptions.IgnoreCase);
}
#endregion

#Region IsPostfix (whether to specify a suffix)
/// <summary>
/// Whether to specify a suffix
/// </summary>
/// <param name="value">string </param>
/// <param name="postfixs">array of suffix names </param>
/// <returns></returns>
public static bool IsPostfix(string value, string[] postfixs)
{
    if (value.IsEmpty())
    {
        return false;
    }
    string postfix = string.Join("|", postfixs);
    return value.IsMatch(string.Format(@".(?i:{0})$", postfix));
}
#endregion

#Regional IsRepeat (Repeat or not)
/// <summary>
/// Repeat or not, example: 112, return true
/// </summary>
/// <param name="value">value</param>
/// <returns></returns>
public static bool IsRepeat(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    var array = value.ToCharArray();
    return array.Any(c => array.Count(t => t == c) > 1);
}
#endregion

2.10 Postal Code Verification

#Region IsPostal Code (Postal Code or not)
/// <summary>
/// Postal code, 6 digits
/// </summary>
/// <param name="value">zip code </param>
/// <returns></returns>
public static bool IsPostalCode(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^[1-9]\d{5}$", RegexOptions.IgnoreCase);
}
#endregion

2.11 Chinese Fixed-line Verification

#Regional IsTel
/// <summary>
/// Whether China Telephone, Format: 010-85849685
/// </summary>
/// <param name="value">telephone </param>
/// <returns></returns>
public static bool IsTel(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
}
#endregion

2.12 QQ Number Verification

#Region IsQQ (legal QQ number)
/// <summary>
/// Is it legal QQ number?
/// </summary>
/// <param name="value">QQ number</param>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
public static bool IsQQ(string value)
{
    if (value.IsEmpty())
    {
        return false;
    }
    return value.IsMatch(@"^[1-9][0-9]{4,9}$");
}
#endregion

3. Source Code

Finally, attach the source address.
https://github.com/jianxuanbing/JCE/blob/master/JCE.Utils/Valid.cs

IV. Regular Expressions

4.1 Check Number Expressions

Number: ^[0-9]*$
N-bit number: ^ d{n}$
Number of at least n bits: ^ d{n,}$
m-n digits: ^ d{m,n}$
Numbers with zero and non-zero beginnings: ^ (0 | [1-9] [0-9]*)$
Number with at most two decimal digits at the beginning of a non-zero: ^([1-9][0-9]*)+(. [0-9]{1,2})?$
Positive or negative numbers with 1-2 decimal digits: ^(\-)? d+(.  d {1,2})?$
Positive, negative, and decimal: ^(-|+)?d+(\.\d+)?$
There are two decimal positive real numbers: ^[0-9]+(. [0-9] {2})?$
Positive real numbers with 1-3 decimal digits: ^[0-9]+(. [0-9] {1,3})?$
Nonzero positive integers: ^[1-9] d*$or ^([1-9][0-9]*) {1,3}$or ^+? [1-9][0-9].*$
Non-zero negative integers: ^-[1-9][]0-9"*$or ^-[1-9] d*$
Non-negative integer: ^ d+$or ^ [1-9] d* | 0$
Non-positive integers: ^-[1-9] d* | 0 $or ^(- d+)|(0+))$
Non-negative floating-point numbers: ^ d+(\ d+)? $or ^[1-9]\ d*\. \\\\\\\\\\\\$
Non-positive floating point numbers: ^(-d+(\d+)|(0+(0+)?)$or ^(-([1-9]d*\d*)\d*| 0\d*|0\\d*)|0?\0+|0?\0.$
Positive Floating Point: ^[1-9]\ d*\ d* | 0\ d*  d*  d*  0\ d* [1-9]\ d*$or ([0-9]+\. [0-9]* [1-9] [0-9]] [0-9]] *)|([0-9] * \ 0-9] ([0-9]]]] ([0-9] * \9]]]] ([0-9]]] $
Negative Floating Point: ^-([1-9]\ d*\. \\\\ d* | 0\\ d* [1-9]\ d*)$or ^(-([0-9]+\ 0-9]* [1-9] [0-9]]] * ([0-9]]]] ([0-9]] * ([0-9]]]]]] ([0-9]]]]]] ([0-9]]] * ([0-9]]]$
Floating point: ^(-?d+)(\d+)(\d+)?$or ^-?([1-9]\d*\d*\d*\0\d*0)\d*[1-9]\d*|0?\0+|0)$

4.2 Check Character Expressions

Chinese characters: ^[4e00- 9fa5] {0,}$
English and figures: ^ [A-Za-z0-9]+$or ^ [A-Za-z0-9]{4,40}$
All characters of length 3-20: ^. {3,20}$
A string of 26 English letters: ^[A-Za-z]+$
A string of 26 capital letters: ^[A-Z]+$
A string of 26 lowercase letters: ^[a-z]+$
A string of numbers and 26 English letters: ^[A-Za-z0-9]+$
A string of numbers, 26 English letters or underscores: ^\ w+$or ^ w{3,20}$
Chinese, English and numerals include underscores: ^[4E00- 9FA5A-Za-z0-9_]+$
Chinese, English, numerals but excluding underscores and other symbols: ^[4E00- 9FA5A-Za-z0-9]+$or ^[4E00- 9FA5A-Za-z0-9] {2,20}$
You can enter characters containing ^%&', =?$"and so on: [^%&', =?$ x22].+
Prohibit input of characters containing [^ \ x22] [+

4.3 Special Requirements Expressions

Email address: ^ w+([-+.] w+)* @ w+([-.] w+)*  w+([-.] w+)* \.  w+([-.] w+)**$
Domain name: [a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/?
Internet URL: [a-zA-z]+: //[^ s]* or ^ http://([ w-]+\.)+[ w-]+ (/[ w-. /?%=*)??$
Mobile phone number: ^ (13 [0-9] | 14 [5 | 7] | 15 [0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9] | 18 [0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9]\d {8}$
Telephone numbers ("XXX-XXXXXXX", "XXXXXXXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXX XXXX", "XXXXXXXXX", "XXXXXXXXX" and "XXXXXXXX XXXX XXXXXXXX":^(\\\\\\\\\\$ 
Domestic telephone numbers (0511-440522, 021-878822):  d {3} -  d {8}  d {4} -  d {7}
ID number (15 digits, 18 digits): ^ D {15} | D {18}$
Short ID number (number, end of letter x): ^([0-9] {7,18} (x | X)? $or ^\ d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
Is the account legitimate (letters start, 5-16 bytes allowed, letters and numbers underlined): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Passwords (starting with letters, ranging in length from 6 to 18 and containing only letters, numbers and underscores): ^[a-zA-Z]\w{5,17}$
Strong passwords (must contain a combination of upper and lower case letters and numbers, not special characters, between 8 and 10 in length): ^(?=. * d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$  
Date format: ^ D {4} -  d {1,2} -  d {1,2}
12 months of a year (01-09 and 1-12): ^(0?[1-9] | 1[0-2])$
31 days of a month (01-09 and 1-31): ^((0?[1-9]) |((1 | 2) [0-9]) | 30 | 31)$ 
Input format of money:
X M L file: ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$
Regular expressions for Chinese characters: [_ 4e00- 9fa5]
Double-byte characters: [^ x00- xff] (including Chinese characters, can be used to calculate the length of the string (a double-byte character length of 2, ASCII character of 1))
Regular expression for blank lines:  n s* R (can be used to delete blank lines)
Regular expressions for HTML tags: <( S*?) [^>]*>. *?</ 1> <. *?/> (The version circulated on the Internet is too bad, and this is only part of it, and it is still powerless for complex nested tags)
Regular expressions for header and tail blank characters: ^ s * | s *$or (^ s *) |( s *$)( can be used to delete blank characters at the beginning and end of a line (including spaces, tabs, page breaks, etc.), very useful expressions)
Tencent QQ signal: [1-9] [0-9] {4,} (Tencent QQ signal from 10000)
China Postal Code: [1-9] D {5}(?! d) (China Postal Code is 6 digits)
IP Address:  d+ d+  d+  d+  d+  d+  d+ (useful for extracting IP Address)
IP Address: (?:(?:(?:25[0-5] | 2 [0-4]\\ D | [01]?\\ d?\ d)\ 3}(?:25 [0-5] | 2 [0-4]\\\ d)?\

Input Format of 4.4 Money

There are four forms of money that we can accept: "10000.00" and "10,000.00", and "10000" and "10,000" without "points": ^ [1-9] [0-9].*$
This means any number that does not start with zero, but it also means that a character "0" does not pass, so we use the following form: ^ (0 | [1-9] [0-9]*)$
A zero or a number that does not start with zero. We can also allow a negative sign at the beginning: ^ (0 | -? [1-9] [0-9]*)$
This means a number with a zero or a possible negative beginning that is not zero. Let the user start with zero. Remove the negative sign, because money can't always be negative. Next we want to add the decimal part: ^[0-9]+(. [0-9]+)?$
It must be noted that there should be at least one digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^ [0-9]+(. [0-9] {2})?$
So we stipulate that there must be two decimal places after the decimal point. If you think it's too harsh, you can do this: ^[0-9]+(. [0-9] {1,2})?$
This allows users to write only one decimal. Next we should consider commas in numbers. We can do this: ^[0-9] {1,3}(, [0-9] {3})*(. [0-9] {1,2})?$
One to three numbers, followed by any comma + three numbers, commas become optional, not necessarily: ^([0-9]+ | [0-9] {1,3} (,[0-9] {3})* (. [0-9] {1,2})??$
Note: This is the end result. Don't forget that "+" can be replaced by "*" if you think empty strings are acceptable (strange, why?). Finally, don't forget to remove the backslash when using the function. Here are the general errors.

Tool Station

On-line regular expression verification

Posted by satanclaus on Sun, 16 Jun 2019 16:13:09 -0700