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 ();}}}