0

I have a string that looks like this:

newNodeXML = "<item id="qDf73w8emTg" parent_id="weLPzE243de" type="suite">
                 <content>
                    <name>Three</name>
                 </content>
              </item>"

In my [WebMethod], I am trying to replace the parent_ID (randomly generated during run-time) like this:

Regex myRegex = new Regex(@""" parent_id=""(.*?)"" type=""");
newNodeXML = myRegex.Replace(newNodeXML, "d43df2qT45");

Please NOTE that for example/demo sake I have used 'd43df2qT45' in the second line above. I will actually be generating that randomly too.

My problem here is that the result of this comes out to be. I do not want this:

<item id="qDf73w8emTgd43df2qT45suite">
   <content>
      <name>Three</name>
   </content>
</item>

Instead, this is what I want it to be:

<item id="qDf73w8emTg" parent_ID="d43df2qT45" type="suite">
   <content>
      <name>Three</name>
   </content>
</item>

P.S. I have tried some examples/google searches and all I could find were examples that got me this far.

6
  • 3
    Regex is probably the wrong way of doing this. Check out stackoverflow.com/questions/2424613/… Commented Jan 1, 2015 at 18:59
  • Is the length of parent_id value fixed and known beforehead? Commented Jan 1, 2015 at 19:01
  • Why don't you deserialize this content, change what you need and then serialize it again? Commented Jan 1, 2015 at 19:01
  • Are you looking specifically for a regex solution? Commented Jan 1, 2015 at 19:03
  • I am fine with any solution to tackle this. Doesn't have to specifically regex. But I guess @Guffa's answer just worked. If anyone thinks regex is not the most efficient way to approach this problem, please feel free to suggest alternatives. Commented Jan 1, 2015 at 19:12

5 Answers 5

6

If you have a known XML structure, using XML tools is probably better idea than using refex, and faster too. For example:

var doc = XDocument.Parse(newNodeXML);
doc.Root.Attribute("parent_id").Value = "xyz";

This code relies on the exact structure you provided. So there's only one item, it's the root of an XML file and it has an attribute called parent_id.

More about the XDocument type on MSDN.

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

2 Comments

Actually, using a regular expression is about twice as fast as parsing the string as XML.
@Guffa I haven't test it, you might be right for this particular scenario. But from my experience, XML performs much better than regex for larger documents. But speed is not the only reason I would not go with regex. Readability is another one. Also I'm always trying to suggest solutions that OP might not be aware of in order to learn him something new :)
0

Since you've hardcoded everything, there is really no need to use capture group's.
Just extend the replacement to this:

"\" parent_id=\"" + "d43df2qT45" + "\" type=\""

Comments

0

You can try with this, I tested it and it seems to work:

String newXml = Regex.Replace(xml, "parent_id=\".+\" ", "parent_id=\"" + newID + "\" ");

Here is a sample method to test it:

String xml = "<item id=\"qDf73w8emTg\" parent_id=\"weLPzE243de\" type=\"suite\">\n\t<content>\n\t\t<name>Three</name>\n\t</content>\n</item>";
String newID = "This is the new parent_Id";
Console.WriteLine("Old xml: \n\n" + xml + "\n\n\nNew xml:\n");
String newXml = Regex.Replace(xml, "parent_id=\".+\" ", "parent_id=\"" + newID + "\" ");
Console.WriteLine(newXml);
Console.ReadKey();

Just paste it inside the main method of a console app, and include the RegularExpression library :)

Comments

0

You can use regex for what your wanting to do by using a regex like below.

Regex myRegex = new Regex("parent_id=\"([^""]+)\");
string myXml = "<myxml>data</myxml>";
xdoc.LoadXml(myXml);

Comments

-1

You can use parentheses to catch the part before and after the value, then use $1 and $2 to include them in the replacement:

Regex myRegex = new Regex(@"( parent_id="")[^""]+("")");
newNodeXML = myRegex.Replace(newNodeXML, "$1" + "d43df2qT45" + "$2");

You can also do it using just string operations:

int pos1 = newNodeXML.IndexOf(" parent_id=\"") + 12;
int pos2 = newNodeXML.IndexOf('"', pos1);
newNodeXML = newNodeXML.Substring(0, pos1) + "d43df2qT45" + newNodeXML.Substring(pos2);

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.