How do I escape a backslash in the following regex c# -
here function, i'm trying replace string in file, c# tells me regex malformed. ideas?
public void function(string filename, string path) { string pathtoammend = @"$serverroot\pathpath"; string newpath = @"$serverroot\" + path; file.writealltext(filename, regex.replace(file.readalltext(filename), pathtoammend, newpath)); .... }
it works if change strings to:
string pathtoammend = @"$serverroot\\pathpath"; string newpath = @"$serverroot\\" + path;
but have 2 slashes , want 1 slash.
a \
special escaping character in regular expressions. have escape interpreted literal \
, not escape sequence. $
special character (an end anchor), you'll want escape well.
string pathtoammend = @"\$serverroot\\pathpath";
using @
create verbatim string means don't have escape \
sake of c# compiler. still have escape \
in regular expression pattern. without verbatim string be:
string pathtoammend = "\\$serverroot\\\\pathpath";
of course, jon skeet points out, simple, regular expressions aren't best way go here.
Comments
Post a Comment