简体   繁体   中英

How can I loop through a string, replacing sections that match a pattern?

I have the following HTML markup

<p>xxxx</p>
 <pre>xxx</pre>
 <p>xxxx</p>
 <pre>yyy</pre>

I need to be able to change this to:

<p>xxxx</p>
 <pre>ABC xxx ABC</pre>
 <p>xxxx</p>
 <pre>ABC yyy ABC </pre>

I had a suggestion to use:

   var loDoc = XDocument.Parse(lcHTML);
   foreach (XElement loElement in loDoc.Descendants("pre"))

This does extract all the pre elements but it doesn't give me a way to tie things together and reinsert code into the original string.

Is there another way I could do this that would allow me to make the code change I need. I was thinking of using split and splitting on the <pre>..</pre> but then that would not really give me what I need as I need to replace the code inside the <pre>...</pre>

One possibility is to use XDocument but it has to be valid XHTML and you need to introduce a root node:

public class Program
{
    static void Main()
    {
        var doc = XDocument.Parse(
            @"<html>
              <p>xxxx</p>
              <pre>xxx</pre>
              <p>xxxx</p>
              <pre>yyy</pre>
            </html>"
        );
        foreach (var pre in doc.Descendants("pre"))
        {
            pre.Value = string.Format("ABC {0} ABC", pre.Value);         
        }
        Console.WriteLine(doc);
    }
}

Another possibility is to use Html Agility Pack :

public class Program
{
    static void Main()
    {
        var doc = new HtmlDocument();
        doc.LoadHtml(
            @"<p>xxxx</p>
              <pre>xxx</pre>
              <p>xxxx</p>
              <pre>yyy</pre>"
        );
        foreach (var pre in doc.DocumentNode.Descendants("pre"))
        {
            pre.InnerHtml = string.Format("ABC {0} ABC", pre.InnerHtml);         
        }
        Console.WriteLine(doc.DocumentNode.OuterHtml);
    }
}

Get the XML Doc element from this link How to read HTML as XML?

using the doc element, try

XmlElement root = doc.DocumentElement; 

XmlNodeList nodes = root.SelectNodes("pre"); 

foreach (XmlNode node in nodes) {
   node.value = "ABC" + node.value + "ABC";
} 

How about using Strings instead of xml?

String xmlString = ... \\ get string representation from somewhere
xmlString = xmlString.Replace( "<pre>", "<pre>ABC " ); 
xmlString = xmlString.Replace( "</pre>", " ABC </pre>" ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM