c# - Regular expression pattern to find a wildcard sub-string in a string -
i new regular expressions , know pattern find if wildcard string exists in string? eg:
if * search_string in main_string (if main string ends search_string)
if search_string * in main_string (if main string starts search_string)
if * search_string * in main_string (if main string contains search_string)
if search_string in main_string (if main string search_string)
search_string can string, "(13786213" or "34576(13786213", should work in case of strings?
i tried this, not generic one, fails "(13786213" case etc.
string search_string = search_string.replace("*", ".*?"); if(new regex(search_string.tolower()).ismatch(main_string)) { \\success }
you may split *
, escape each chunk, , join .*?
.
var search_string = string.join(".*", search_string.split('*').select(v => regex.escape(v)));
then, make case insensitive search, compile regex
object regexoptions.ignorecase
flag.
here c# demo:
var search_string = "(137*86213"; search_string = string.join(".*", search_string.split('*').select(v => regex.escape(v))); if(regex.ismatch("(137 text here 86213", search_string, regexoptions.ignorecase)) { console.write("success"); } // => success
if input strings can contain newlines, add regexoptions.singleline
option.
Comments
Post a Comment