C # Regular Expression open-source tool

Source: Internet
Author: User
First to explain the background, the recent work often use regular expressions, and the regular expression of this thing I personally feel very chicken, do not, some of the functions can be very cumbersome to achieve. Use it, not to say that work is often used, but sometimes some need to ask for it. But regular expressions can be forgotten, even forgotten, as long as they are not used for a period of time. To solve this problem to some extent, there is the idea of this blog and the open source regular expression validation tool that I intend to write. About the regular online information is actually a lot, the reason why this blog is only to record some of the current or future work may be used in the regular, and then share it, but also want to do something for. NET open source.

0. Write in front

Remember the last time serious used regular expression or three years ago, although the period also fragmented used some, but the basic has been forgotten almost, so this blog if there is something wrong place welcome and thank you correct! Here I just wrote some of the regular matches I used in my personal work, and I would be very welcome if I had a friend who was willing to contribute to the code of the regular open source tool.

Recently the garden of "menstruation paste" more and more, I am very puzzled, this group of people is not idle egg ache? You have time to criticize the language in this "Jiangshan" attack on the environment, you might as well open your hands to embrace. NET open source. With the advent of. NET open source,. NET spring is coming, we should reach out and do something for. NET open source, and let the. NET community become more and more open source. Of course, this is a digression, those menstruation stickers is not my concern.

V1. SOURCE Address

Https://github.com/toutouge/TouTou.RegexTool back to the top of the

V2. Body Start

2.1.: Generic Match

C # Universal matching rules that can be passed to the source string and the corresponding regular when called

<summary>///detect if a regular subset is included in the string,//</summary>///<param name= "source" > Source string </param>/// <param name= "Reg" > regular, e.g. \d+</param>///<returns>true: Included, not including </returns>publicbool Checkcontainsbyreg (string source, String reg) {Returnregex.match (source, Reg). Success;} &LT;SUMMARY&GT;///detects whether the entire string matches the regular, rather than including//</summary>///<param name= "source" > String </param>/// <param name= "Reg" > regular, e.g. ^\d+$</param>///<returns>true: match, inverse mismatch </returns>publicbool Checkstringbyreg (string source, String reg) {Regex RG = Newregex (Reg, regexoptions.ignorecase); return RG. IsMatch (source);} <summary>///filters the first subset that matches a regular match from a specified string//</summary>///<param name= "source" > Source string </param>/ <param name= "Reg" > regular, e.g. \d+</param>///<returns> the first matching subset of the source string </returns>publicstring Getfirststringbyreg (string source, String reg) {Returnregex.match (source, Reg). Groups[0]. Value;} <summary>/Filters out all matching subsets of a regular match from the specified string//</summary>///<param name= "source" > </param>///<param "reg "> regular, e.g. \d+</param>///<returns>true: match, inverse mismatch </returns>publicList<string> Getstringbyreg (string source, String reg) {var regex = regex.matches (source, Reg); List<string> list =newlist<string> (); foreach (Match item in regex) {list. ADD (item. Value);} return list;}

2.2.: Digital Match

C # regular Expressions match numbers according to various requirements

<summary>///filter out the first digit from the specified string///</summary>///<param name= "source" > Origin string </param>///< Returns> the first digit of the source string </returns>publicstring getfirstnumberbystring (string source) {Returnregex. Match (source, @ "\d+"). Groups[0]. Value;} <summary>///filter out the last digit from the specified string///</summary>///<param name= "source" > Origin string </param>///< Returns> the last digit of the source string </returns>publicstring getlastnumberbystring (string source) {var reg = Regex.Matches ( SOURCE, @ "\d+"); return Reg[reg. COUNT-1]. Value;} <summary>///all the numbers from the specified string,//</summary>///<param name= "source" > Source string </param>///< Returns> all digits of the source string </returns>publicList<string> getallnumberbystring (string source) {var Reg = Regex.Matches (source, @ "\d+"); List<string> list =newlist<string> (); foreach (Match item in reg) {list. ADD (item. Value);} return list;} <summary>///whether the source string contains a number///</summary>///<param name= "source" > Sources string </param><returns>true: The source string contains a number, false: The source string does not contain a number </returns>publicbool checknumberbystring (string source) { Returnregex. Match (source, @ "\d"). Success;} <summary>///determines whether the string is all numeric and length equals the specified length//</summary>///<param name= "source" > Source string </param>// /<param name= "Length" > Specified length </param>///<returns> return value </returns>publicbool Checklengthbystring (string source, int length) {Regex RG = Newregex (@ "^\d{" + length + "}$"), return RG. IsMatch (source);}

2.3.: Regular intercept string

C # intercepts strings between starts based on a given starting character

<summary>///the string in the middle of the start and end strings in the string///</summary>///<param name= "source" > String </param>/// <param name= "Startstr" > Start string </param>///<param name= "Endstr" > End string </param>///<returns > Intermediate string </returns>publicstring Substring (string source, String startstr, String endstr) {Regex RG = Newregex ("? & lt;= ("+ Startstr +")) [. \\s\\s]*? (? = ("+ endstr +")) ", Regexoptions.multiline | Regexoptions.singleline); return RG. Match (source). Value;}

2.4.: Mailbox Match

C # Regular expression matching mailbox

<summary>///Matching mailbox is legal///</summary>///<param name= "source" > string to be matched </param>///< Returns> Match result True is mailbox is not mailbox </returns>publicbool checkemailbystring (string source) {Regex RG = Newregex ("^\\s* ([a-za-z0-9_-]+ (\\.\\w+) *@ (\\w+\\.) +\\w{2,5}) \\s*$ ", regexoptions.ignorecase); return RG. IsMatch (source);}

2.5.: URL Match

C # regular expression matching URL

<summary>///Match URL is valid///</summary>///<param name= "source" > string to be matched </param>///< Returns> Match result true is the URL is not url</returns>publicbool checkurlbystring (string source) {Regex RG = Newregex (@ "^ ( https?| S?FTP): \/\/(((([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | (%[\da-f]{2}) | [!\$& ' \ (\) \*\+,;=]|:) *@)? (((\d| [1-9]\d|1\d\d|2[0-4]\d|25[0-5]) \. (\d| [1-9]\d|1\d\d|2[0-4]\d|25[0-5]) \. (\d| [1-9]\d|1\d\d|2[0-4]\d|25[0-5]) \. (\d| [1-9]\d|1\d\d|2[0-4]\d|25[0-5]) | ((([a-z]|\d| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | ([a-z]|\d| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) ([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) * ([a-z]|\d| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]))) \.) + ([a-z]| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | ([a-z]| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) ([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) * ([a-z]| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]))) \.?) (: \d*)?) ((([a-z]|\d|-|\.| _|~| [\u00a0-\ud7ff\uf900-\UFDCF\UFDF0-\UFFEF]) | (%[\da-f]{2}) | [!\$& ' \ (\) \*\+,;=]|:| @) + (([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | (%[\da-f]{2}) | [!\$& ' \ (\) \*\+,;=]|:| @)*)*)?)? (\? ((([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | (%[\da-f]{2}) | [!\$& ' \ (\) \*\+,;=]|:| @)| [\ue000-\uf8ff]|\/|\?] *)? (# (([a-z]|\d|-|\.| _|~| [\U00A0-\UD7FF\UF900-\UFDCF\UFDF0-\UFFEF]) | (%[\da-f]{2}) | [!\$& ' \ (\) \*\+,;=]|:| @)|\/|\?) *)? $ ", regexoptions.ignorecase); return RG. IsMatch (source);}

2.6.: Date Matching

C # Regular Expression match date

<summary>///Match Date is valid///</summary>///<param name= "source" > string to be matched </param>///< Returns> Match result True is a date other than date </returns>publicbool checkdatebystring (string source) {Regex RG = Newregex (@ "^ (\d{ 4}[\/\-] (0?[ 1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) | ((0? [1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) [\/\-]\d{4}] $ "); return RG. IsMatch (source);} <summary>///gets the first date from a string//</summary>///<param name= "source" > Source string </param>///< Returns> the first date in a source string </returns>publicstring getfirstdatebystring (string source) {Returnregex.match (source, @ "(\d{4}[\/\-] (0?[ 1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) | ((0? [1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) [\/\-]\d{4}]). Groups[0]. Value;} <summary>///get all the dates from the string///</summary>///<param name= "source" > Source string </param>///< Returns> all dates in the source string </returns>publicList<string> getalldatebystring (string source) {var all = Regex.Matches (Source, @ "(\d{4}[\/\-] (0?[ 1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) | ((0? [1-9]|1[0-2]) [\/\-] ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) [\/\-]\d{4})]; List<string> list =newlist<string> (); foreach (Match item in all) {list. ADD (item. Value);} return list;}

2.7.: Password match

C # Regular expression matching password

<summary>///Detection of Password complexity compliance: The password must contain letters, numbers, special characters, at least 8 characters, and a maximum of 16 characters. </summary>///<param name= "source" > string to be matched </param>///<returns> password Complexity compliance true is compliance and not standard </returns>publicbool checkpasswordbystring (String source) {Regex RG = Newregex (@ "^ (? =.*\d) (? =.*[a-za-z]) (? =.*[ ^A-ZA-Z0-9]). {8,16}$ "); return RG. IsMatch (source);}

2.8.: Zip Match

C # Regular expression matching zip code

<summary>///Match zip///</summary>///<param name= "source" > string to be matched </param>///< Returns> zip code returns TRUE, otherwise illegal </returns>publicbool checkpostcodebystring (string source) {Regex RG = Newregex (@ "^\ d{6}$ "); return RG. IsMatch (source);}

2.9.: Phone number

C # Regular expression matching phone

///<summary>///match Phone number legal//</summary>///<param name= "source > to match string </param>///<returns> phone number is legal return true, and vice versa </returns>publicbool checktelephonebystring ( String source) {Regex RG = Newregex (@ "^ (\ (\d{3,4}-) |\d{3.4}-)" \d{7,8}$ "); return RG. IsMatch (source);} <summary>///getting the phone number from a string//</summary>///<param name= "source" > Source string </param>///< Returns> the source string in the phone number </returns>publicstring gettelephonebystring (string source) {Returnregex. Match (Source, @ "(\ (\d{3,4}-) |\d{3.4}-)? \d{7,8}"). Groups[0]. Value;} 

2.10.: Mobile phone number

C # Regular expression matches phone number

///<summary>///match mobile phone number is legal///</summary>///<param name= "source > to match string </param>///<returns> cell phone number legal return true, otherwise illegal </returns>publicbool Checkmobilephonebystring (string source) {Regex RG = Newregex (@ "^[1]+[3,5,7]+\d{9}$"); return RG. IsMatch (source);} <summary>///get the phone number from the string///</summary>///<param name= "source" > Source string </param>///< Returns> the phone number in the source string </returns>publicstring getmobilephonebystring (string source) {Returnregex. Match (source, @ "[1]+[3,5,7]+\d{9}"). Groups[0]. Value;} 

2.11.: Identity card Matching

C # Regular expression matches the ID number

///<summary>///match ID number legal//</summary>///<param name= " SOURCE "> string to be matched </param>///<returns> ID number legal return true, otherwise illegal </returns>publicbool Checkidcardbystring (string source) {Regex RG = Newregex (@ "^ (^\d{15}$|^\d{18}$|^\d{17} (\d| x|x)); return RG. IsMatch (source);} <summary>///get the ID number from the string///</summary>///<param name= "source" > Source string </param>///< Returns> the source string in the ID number </returns>publicstring getidcardbystring (string source) {Returnregex. Match (Source, @ "(^\d{15}$|^\d{18}$|^\d{17} (\d| x|x)). Groups[0]. Value;} 

V3. Blog Summary

The Open Source tool for C # Regular expressions only accumulates so much for the time being. Because this is the actual work encountered in this, about this C # Regular expression Open Source tool is now a prototype, the first to share this point, the follow-up will continue to update the C # Regular expression open Source tool. Hope that in the future, this C # Regular expression tool will become more and more strong, but also hope to get the support of the Friends of the park.

The above is the content of the C # Regular expression open Source tool, please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content.

  • 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.