Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove single backslashes, convert doubles to single in string

Tags:

c#

regex

How best can I convert instances of double backslashes to a single backslash in a string, but remove any occurrences of single backslash?

So this:

\|Testing|ABC:1234\\1000-1\|

Should convert to this:

|Testing|ABC:1234\1000-1|

Ideally I want to avoid a temporary replace of '\' to another character. A solution using .NET or Regular Expressions is preferred.

like image 558
GONeale Avatar asked Dec 17 '25 16:12

GONeale


1 Answers

Regex.Replace(input, @"\\(.|$)", "$1");

[Edit: Pattern didn't match all possible cases specified in the OP. Thanks for the suggestions, GONeale and Alan M.]

like image 182
Juliet Avatar answered Dec 20 '25 08:12

Juliet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!