C # Regular Expression match string

Source: Internet
Author: User

A regular expression can quickly determine whether a given string is a specified format. Here, some common methods are encapsulated into a string tool class.

1  Public Static classStringtool2 {3     /// <summary>4     ///determine if a string is a pure number5     /// </summary>6     /// <param name= "str" ></param>7     /// <returns></returns>8      Public Static BOOLIsnumber (stringstr)9     {Ten         returnRegex.IsMatch (str,@"^[0-9]+$"); One     } A  -     /// <summary> -     ///determine if a string is a pure letter the     /// </summary> -     /// <param name= "str" ></param> -     /// <returns></returns> -      Public Static BOOLIsletter (stringstr) +     { -         returnRegex.IsMatch (str,@"^[a-za-z]+$"); +     } A  at     /// <summary> -     ///determine whether a string is a combination of letters or numbers -     /// </summary> -     /// <param name= "str" ></param> -     /// <returns></returns> -      Public Static BOOLIsletterornumber (stringstr) in     { -         returnRegex.IsMatch (str,@"(? i) ^[0-9a-z]+$"); to     } +  -     /// <summary> the     ///number of Chinese characters in the statistics string *     /// </summary> $     /// <param name= "str" ></param>Panax Notoginseng     /// <returns></returns> -      Public Static intCountchinese (stringstr) the     { +         returnStr. Count (c = Regex.IsMatch (c.tostring),@"^[\u4e00-\u9fa5]{0,}$")); A     } the  +     /// <summary> -     ///determine if a string is plain Chinese $     /// </summary> $     /// <param name= "str" ></param> -     /// <returns></returns> -      Public Static BOOLIschinese (stringstr) the     { -         returnRegex.IsMatch (str,@"^[\u4e00-\u9fa5],{0,}$");Wuyi     } the  -     /// <summary> Wu     ///determines whether the string contains Chinese -     /// </summary> About     /// <param name= "str" ></param> $     /// <returns></returns> -      Public Static BOOLHaschinese (stringstr) -     { -         returnRegex.IsMatch (str,@"[\u4e00-\u9fa5]"); A     } +  the     /// <summary> -     ///number of full-width characters in the statistics string $     /// </summary> the     /// <param name= "str" ></param> the     /// <returns></returns> the      Public Static intCountsbccase (stringstr) the     { -         returnEncoding.Default.GetByteCount (str)-Str. Length; in     } the  the     /// <summary> About     ///determines whether the string contains full-width characters the     /// </summary> the     /// <param name= "str" ></param> the     /// <returns></returns> +      Public Static BOOLHassbccase (stringstr) -     { the         returnCountsbccase (str) >0;Bayi     } the  the     /// <summary> -     ///number of half-width characters in the statistics string -     /// </summary> the     /// <param name= "str" ></param> the     /// <returns></returns> the      Public Static intCountdbccase (stringstr) the     { -         returnStr. Length-countsbccase (str); the     } the  the     /// <summary>94     ///determines whether the string contains half-width characters the     /// </summary> the     /// <param name= "str" ></param> the     /// <returns></returns>98      Public Static BOOLHasdbccase (stringstr) About     { -         returnCountdbccase (str) >0;101     }102 103     /// <summary>104     ///determine if a string is compliant with the mailbox format the     /// </summary>106     /// <param name= "str" ></param>107     /// <returns></returns>108      Public Static BOOLIsemail (stringstr)109     { the         returnRegex.IsMatch (str,@"^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$");111     } the}

C # Regular Expression match string

Related Article

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.