Regex.Replace () to replace the whole event

I use regex.Replace () to replace all occurrences of the string .. so I gave as Regex.Replace (str, @stringToReplace, "**"); where stringToReplace = @ "session" + "\ b";

if I give so that it does not replace. but if I give as Regex.Replace (str, @ "session \ b", "**"); then it works .. how to avoid it .. I want to pass a value that will be set dynamically ..

Thanks niai

+4
source share
3 answers

try

stringToReplace = @"session" + @"\b"; 
+4
source

@ here means a literal string literal .

When you write "\ b" without @ , it means a backspace character, that is, a character with ASCII code 8. You need a line consisting of a backslash followed by b , which means the word boundary when in a regular expression.

To get this, you need to either escape the backslash to make it a literal backslash: "\\b" or make the second line also in the text literal of the line: @"\b" . Also note that @ in @"session" (without \b ) actually has no effect, although there is no harm to leave it there.

 stringToReplace = "session" + @"\b"; 
+4
source

@ "session" + "\ b" as well as @ "Sessions \ b"

- not the same line. In the first case, "\ b" you do not consider the slash as a slash, but as an escape parameter. In the second case, you do.

So, @ "session" + @ "\ b" should bring the same result

0
source

Source: https://habr.com/ru/post/1308758/


All Articles