Loading ...

Regex: Replace single backslash with double backslash | CodeAsp.Net

Regex: Replace single backslash with double backslash

 /5
0 (0votes)

In this blog I will show how to replace single backslash with double backslash in a string with REGEX. Let's say we have a string "This is \ test \ string" and we want to replace single backslash with double backslash so that the output comes "This is \\ test \\ string". Below is the sample code to do the same:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            const string inputString = @"This is \ test \ string";

            string output = Regex.Replace(inputString, @"\\", @"\\");

            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}

Output:

Below is the explanation of regex:

\\

Match the character “\” literally «\\»


Created with RegexBuddy

Comments (no comments yet)

Top Posts