Share a regular expression help class

Source: Internet
Author: User
Using system. Globalization;
Using system. Text. regularexpressions;

/// <Summary>
/// Regular Expression help class
/// </Summary>
Public sealed class regexhelper
{
Private regexhelper ()
{
}

/// <Summary>
/// Clear the inclusion string
/// </Summary>
Public const string clean_string = @ "[']";

/// <Summary>
/// Verify whether the string is between the character begin-end
/// </Summary>
Public const string is_valid_byte = @ "^ [A-Za-z0-9] {#0 #, #1 #} $ ";

/// <Summary>
/// Verify whether the string is year month day
/// </Summary>
Public const string is_valid_date =
@ "^ 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) $ ";

/// <Summary>
/// Verify whether the string is a decimal number
/// </Summary>
Public const string is_valid_decimal = @ "[0]. \ D {1, 2} | [1]";

/// <Summary>
/// Verify whether the string is email
/// </Summary>
Public const string is_valid_email =
@ "^ ([\ W-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ W-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ";

/// <Summary>
/// Verify whether the string is an IP address
/// </Summary>
Public const string is_valid_ip =
@ "^ (\ 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]) $ ";

/// <Summary>
/// Verify that the string is a suffix
/// </Summary>
Public const string is_valid_postfix = @"\.(? I: {0}) $ ";

/// <Summary>
/// Verify whether the string is a phone number
/// </Summary>
Public const string is_valid_tel = @ "(\ D + -)? (\ D {4 }-? \ D {7} | \ D {3 }-? \ D {8} | ^ \ D {7, 8}) (-\ D + )? ";

/// <Summary>
/// Verify whether the string is a URL
/// </Summary>
Public const string is_valid_url = @ "^ [A-Za-Z] +: // (\ W + (-\ W + )*)(\\. (\ W + (-\ W + )*))*(\\? \ S *)? $ ";

# Region replacement string
/// <Summary>
/// Replace the string
/// </Summary>
/// <Param name = "input"> input string </param>
/// <Param name = "RegEx"> Regular Expression </param>
/// <Returns> string after replacement </returns>
Public static string replaceinput (string input, string RegEx)
{
Return RegEx. Replace (input, RegEx, String. Empty );
}

/// <Summary>
/// Replace the string
/// </Summary>
/// <Param name = "input"> input string </param>
/// <Param name = "RegEx"> Regular Expression </param>
/// <Param name = "replace"> replace string </param>
/// <Returns> string after replacement </returns>
Public static string replaceinput (string input, string RegEx, string replace)
{
Return RegEx. Replace (input, RegEx, replace );
}

# Endregion

# Region verification string

/// <Summary>
/// Verify the string
/// </Summary>
/// <Param name = "input"> input string </param>
/// <Param name = "RegEx"> Regular Expression </param>
/// <Returns> whether the verification succeeds </returns>
Public static bool checkinput (string input, string RegEx)
{
Return RegEx. ismatch (input, RegEx );
}

# Endregion

# Region common methods

/// <Summary>
/// Verify the string
/// </Summary>
/// <Param name = "input"> input string </param>
/// <Param name = "RegEx"> Regular Expression </param>
/// <Param name = "begin"> Start number </param>
/// <Param name = "end"> end number </param>
/// <Returns> whether the verification succeeds </returns>
Public static bool validbyte (string input, string RegEx, int begin, int end)
{
Bool ret = false;
If (! String. isnullorempty (RegEx ))
{
String rep = RegEx. Replace ("#0 #", begin. tostring (cultureinfo. invariantculture ));
Rep = rep. Replace ("#1 #", end. tostring (cultureinfo. invariantculture ));
Ret = checkinput (input, Rep );
}
Return ret;
}

/// <Summary>
/// Verify the string
/// </Summary>
/// <Param name = "input"> input string </param>
/// <Param name = "RegEx"> Regular Expression </param>
/// <Param name = "fix"> suffix </param>
/// <Returns> whether the verification succeeds </returns>
Public static bool validpostfix (string input, string RegEx, string fix)
{
String ret = string. Format (cultureinfo. invariantculture, RegEx, fix );
Return checkinput (input, RET );
}

# Endregion


}

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.