11

I have a text, the text contains char sequence which begins with # and ends with ="" characters. Between the # and ="" characters just exists alphanumeric characters, without space or ; chars. I tried to find the specified sequence with the following pattern #[A-Za-z0-9]+(="") My question is how can I replace the ="" characters with three question mark characters ??? in C#?

Thank you in advance.

0

1 Answer 1

21

The correct way of doing what you need is to capture the part you need to keep and only match what you need to replace:

var result = Regex.Replace(s, "(#[A-Za-z0-9]+)=\"\"", "$1???");

See the regex demo.

In the (#[A-Za-z0-9]+)="" pattern, the # and alphanumeric chars are captured into Group 1 and later are re-inserted into the resulting string with the help of the replacement backreference $1 (also called a placeholder for Group 1). Since ="" is a string of a known length, you may safely put three ? chars after the $1.

If you have no control over the pattern and just need to replace the contents of the first group, and in case you do not know the length of Group 2 value (it is not the case, but let's generalize), you may consider the following approach:

var s = "#Abc=\"\"";
var result = Regex.Replace(s, "#[A-Za-z0-9]+(=\"\")", m=> 
    string.Format("{0}{1}{2}", 
       m.Value.Substring(0, m.Groups[1].Index), // Get the substring up to Group 1 value
       new string('?', m.Groups[1].Length), // Build the string of Group 1 length ?s
       m.Value.Substring(m.Groups[1].Index+m.Groups[1].Length,  m.Value.Length-m.Groups[1].Index-m.Groups[1].Length))); // Append the rest of the match
Console.WriteLine(result);

See this C# demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.