Replace character but provide proper escaping

2076 views c#
4

I want to replace a & character with a § character but provide a proper way to escape the & sign. I want to use the character \ for escaping but also to escape itself.

So basically:

Input:

&1 \&2 \\&3 \\\&4 \\  

Output:

§1 &2 \§3 \&4 \  

It seems like Regex is the proper way to do this but I can't get it working.
This is how far my Regex is for the & signs:

(?<=(?<!\\)(?:\\){2}*)&.

I want to use this for C#, if this should be important.

answered question

@wiktor-stribi?ew I just realized my edit screwed up the question, thanks for the edit

Well, I am still wondering why the required output is like that. Shouldn't it be §1 \&2 \\§3 \\\&4 \\

@RuiJarimba \& has one backslash for escaping the & and should not be replaced with §, but \\&3 has two backslashes, so the backslashes will escape itself and show \§3. It is like how you would escape the quote in most languages (\" would be " in a string, \\" would be a backslash and the end of the string.). I hope that you understand what I meant now.

So, you want to replace & and then "shrink" double backslashes? var res = Regex.Replace(text, @"(?<=(?<!\\)(?:\\{2})*)&", "§").Replace("\\\\", "\\")?

@WiktorStribi?ew Because I want to use the \ for escaping, so \\ would become a single \ , \& would become & and & should become §. It is like a string in C# (or most other languages) "\" \\\"\\" would become `" \"`

@WiktorStribi?ew Not exactly, your code would give the output `§1 \&2 \§3 \\&4 `

1 Answer

7

Is it really necessary to use Regex?

You could use following code snippet:

var output = new StringBuilder();
bool escape = false;
for (var i = 0; i < input.Length; ++i)
{
    if (escape)
    {
        output.Append(input[i]);
        escape = false;
    }
    else
    {
        switch (input[i])
        {
            case '\\':
                escape = true;
                break;
            case '&':
                output.Append('§');
                break;
            default:
                output.Append(input[i]);
                break;
        }
    }
}

input is a string with escape symbols, e.g. &1 \&2 \\&3 \\\&4 \\

output.ToString() will contain the correct string.

posted this

Have an answer?

JD

Please login first before posting an answer.