I don't like RegEx...

Source: Internet
Author: User
Tags net regex

Refer to: http://www.codeproject.com/Articles/368258/I-dont-like-Regex

String extension method class:

Using system; using system. collections. generic; using system. text. regularexpressions; namespace regexextract {static class stringextract {public static bool like (this string item, string searchpattern) {return getregex ("^" + searchpattern ). ismatch (item);} public static string search (this string item, string searchpattern) {var match = getregex (searchpattern ). match (item); If (match. success) {retur N item. substring (match. index, match. length);} return NULL;} public static list <string> extract (this string item, string searchpattern) {var result = item. search (searchpattern); If (! String. isnullorwhitespace (result) {var splitted = searchpattern. Split (new [] {'? ',' % ',' * ',' # '}, Stringsplitoptions. removeemptyentries); var temp = result; var final = new list <string> (); array. foreach (splitted, x => {var Pos = temp. indexof (x, stringcomparison. ordinal); If (Pos> 0) {final. add (temp. substring (0, POS); temp = temp. substring (POS);} temp = temp. substring (X. length) ;}); If (temp. length> 0) Final. add (temp); Return final;} return NULL;} // Private method wh Ich accepts the simplified pattern and transform it into a valid. net RegEx pattern: // It uses standard RegEx syntax reserved characters // and transforms the simplified syntax into the native RegEx one static RegEx getregex (string searchpattern) {return New RegEx (searchpattern. replace ("\\","\\\\"). replace (". ","\\. "). replace ("{","\\{"). replace ("}","\\}"). replace ("[","\\["). replace ("]", "\]"). Replace ("+", "\ + "). replace ("$", "\ $ "). replace ("", "\ s "). replace ("#", "[0-9]"). replace ("? ",". "). Replace ("*", "\ W *"). replace ("% ",". * "), regexoptions. ignorecase );}}}

test case:

Using system; namespace regexextract {class program {static void main () {// example: a string is a guid var result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495 ". like ("????????? -???? -???? -???? -???????????? "); Console. writeline (result); // example: a string starts with a guid result = "This Is a guid TA0E02391-A0DF-4772-B39A-C11F7D63C495 ". like ("% ????????? -???? -???? -???? -???????????? "); Console. writeline (result); // example: a string ends with a guid result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495 is a guid ". like ("????????? -???? -???? -???? -???????????? % "); Console. writeline (result); // example: a string contains a guid result = "this string TA0E02391-A0DF-4772-B39A-C11F7D63C495 contains a guid ". like ("% ????????? -???? -???? -???? -???????????? % "); Console. writeline (result); // example: search for a guid inside a text var strresult = "this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching ". search ("[????????? -???? -???? -???? -???] "); Console. writeline (strresult); // output: [TA0E02391-A0DF-4772-B39A-C11F7D63C495] // example: retrieving the consituents of a guid inside a text var listresult = "this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching ". extract ("[????????? -???? -???? -???? -???] "); Foreach (var str in listresult) {console. writeline (STR);} // result is an array containing each part of the pattern: {"ta0e02391", "a0df", "4772", "b39a ", "c11f7d63c495"} // example: retrieving the consituents of an email inside a text listresult = "this string contains an email: toto@domain.com ". extract ("*? @? *.? * "); Foreach (var str in listresult) {console. writeline (STR);} // result is an array containing each part of the pattern: {"Toto", "Domain", "com"} console. readkey ();}}}

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.