c# - Regex.Escape() unrecognized exception -
i have following code in c#
:
string str = @"\hello ali how you? fine? hello , hi"; console.writeline(regex.matches(regex.escape(str), @"\hello").count);
i following error:
{"parsing \"\hello\" - unrecognized escape sequence \h."}
i need know when \ + (some names)
exists in code. have used @
not using escape sequence characters still mentioned error. can not understand why happens!
escape sequences used independently in both strings and regular expressions different meanings. when prefix string literal @
telling compiler interpret string literally without escape sequences. regular expression engine again sees "\h" , tries interpret escape sequence.
basically need apply both string literal @
and regex.escape
function make \h
interpreted literal both parsers:
console.writeline(regex.matches(str, regex.escape(@"\hello")).count);
Comments
Post a Comment