2

How can I use Regex Replace with XML?

Assuming I have the given text below:

<test>Hello World</test>
<test1>Hello World</test1>
<test2>Hello World</test2>
<test3>Hellow World</test3>

How can I replace all "Hello World" tag values if the parent tag is not test tag?

roybalderama
  • 1,650
  • 21
  • 38
  • 3
    http://stackoverflow.com/a/1732454/1726343 – Asad Saeeduddin Nov 14 '12 at 06:21
  • Do you want it to replace it only if the tag value is "Hello World"? Or you want to replace it with a fixed content only if the tag is "test" regardless of the content? – Sina Iravanian Nov 14 '12 at 06:26
  • sorry for the confusion, what I intended to do is change all hello world value if the parent tag is not test...Thanks – roybalderama Nov 14 '12 at 06:30
  • @Default Yes, this is XML, and many of the same rules apply. XML is less fuzzy, but you can't parse it without resorting to things like recursion in your regex, in which case your regex no longer fits in the category of finite automata – Asad Saeeduddin Nov 14 '12 at 07:49

2 Answers2

3

Regex can be used for parsing XML since it is strict with its format but it is not recommended to use it

Use LINQ2XML

XElement doc=XElement.Load("yourXML.xml");
foreach(XElement elm in doc.Descendants().Elements().Where(x=>x.Name.LocalName!="test"))
{
     if(elm.Value=="hello World")
          elm.Value="";//replace here
}

doc.Save("yourXml.xml");
Anirudha
  • 32,393
  • 7
  • 68
  • 89
1

Try this Regex using negative lookahead and negative lookbehind assertion:

(?<!<test>)(Hello World)(?!</test>)

explain:

(?<! subexpression ) Zero-width negative lookbehind assertion.

(?! subexpression ) Zero-width negative lookahead assertion.

And replace with your string using Regex.Replace:

Regex.Replace(inputString, "(?<!<test>)(Hello World)(?!</test>)", "New World");
Ria
  • 10,237
  • 3
  • 33
  • 60