0

I just want to replace a portion of a string only if matches the given text. My use case is as follows:

    var text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
    string result = text.Replace("wd:response", "response");

    /*
     * expecting the below text
      <response><wd:response-data></wd:response-data></response>
     *
     */

I followed the following answers:

Way to have String.Replace only hit "whole words"

Regular expression for exact match of a string

But I failed to achieve what I want.

Please share your thoughts/solutions.

Sample on https://dotnetfiddle.net/pMkO8Q

RSF
  • 510
  • 6
  • 18
  • 1
    This seems more a question how to remove the namespace from an XML element. Doing this with `string.Replace` or Regex might have unexpected consequences. Are you going on to read this XML or are you simply mutating and saving? – ProgrammingLlama May 30 '19 at 05:56
  • Hi @John, Thanks for your prompt reply. I'm just mutating and saving on my local. I just want the xml document without the ns. any thoughts? – RSF May 30 '19 at 05:59
  • https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c – TheGeneral May 30 '19 at 06:04
  • @RSF that's Word XML, not just a string. Never mind that string manipulation is a bad way to deal with XML, *document processing* is more than just modifying XML elements. – Panagiotis Kanavos May 30 '19 at 06:39
  • @RSF as for the replacement you want to perform *why*? There's nothing wrong with namespaces in XML. Removing the namespace will result in *invalid* XML and a broken document. .NET's XML classes and the Open XML SDK can handle namespaces without any problems – Panagiotis Kanavos May 30 '19 at 06:40
  • @RSF `I just want the xml document without the ns. any thoughts?` why do you want to do that? What is your *actual* question? – Panagiotis Kanavos May 30 '19 at 06:41

3 Answers3

0

In general, you should really be parsing and manipulating XML as XML, using functions that know how XML works and what's legal in the language. Regex and other naive text manipulation will often lead you into trouble.

That said, for a very simple solution to this specific problem, you can do this with two replaces:

var text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
text.Replace("wd:response>", "response>").Replace("wd:response ", "response ")

(Note the spaces at the end of the parameters to the second replace.)

Alternatively use a regex similar to "wd:response\s*>"

jmbpiano
  • 561
  • 3
  • 19
0

You can capture the string wd-response in a capturing group and replace using Regex.Replace using the MatchEvaluator like this.

Regex explanation - <[/]?(wd:response)[\s+]?>

  • Match < literally
  • Match / optionally hence the ?
  • Match the string wd:response and place it in a capturing group enclosed with ()
  • Match one or more optional whitespace [\s+]?
  • Match > literally

public class Program
{
    public static void Main(string[] args)
    {
        string text = "<wd:response><wd:response-data></wd:response-data></wd:response >";
        string replacePattern = "response";
        string pattern = @"<[/]?(wd:response)[\s+]?>";
        string replacedPattern = Regex.Replace(text, pattern, match =>
        {
            // Extract the first group
            Group group = match.Groups[1];

            // Replace the group value with the replacePattern
            return string.Format("{0}{1}{2}", match.Value.Substring(0, group.Index - match.Index), replacePattern, match.Value.Substring(group.Index - match.Index + group.Length));
        });
        Console.WriteLine(replacedPattern);
    }
}

Outputting:

<response><wd:response-data></wd:response-data></response >
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
0

The easiest way to achieve your result as per your .net fiddle is use the replace as below.

string result = text.Replace("wd:response>", "response>");

But proper way to achieve this is parsing using XML