简体   繁体   中英

Regex to remove \n\ from a string in C# (Not \n)

I am looking for a 'Regex' to replace \\n\\ from my string. I already looked thorough this post and other regex posts here, but in my case I need to remove the value \\n\\ from a string not a new line or ( \\n ).

For example:

string eneteredString = @"abc\n\def\n\ghi\n\\";

Here \\\\ can be found multiple times too.

I have already tried Replacing Enviornment.NewLine , as it is not a new line in my case, it also didn't work. When I try below code,

string regout = Regex.Replace(enteredString, @"\n\","");

It says parsing "\\n\\" - Illegal \\ at the end of pattern . Can you anyone please help me with the same. Thanks in advance.

Who need regex?

string regout = @"abc\n\def\n\ghi\n\\".Replace(@"\n\", "");

And just because quotes are cool, here is the famous :

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

use @"\\\\n\\\\" . You need to remove the meaning of the escape character \\ by adding a \\ before it.. Here is a testing image

在此处输入图片说明

This is what you want. The reason is due to how regex handles the single \\

string regout = Regex.Replace(enteredString, @"\\n\\","");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM