简体   繁体   中英

string with HTML - replace elements/part of string (??Regex)

... I try to explain it in another way. I have a string like this:

string myText = "... <p class="MsoNormal">bla gezeichnete bla zuzustellen.</p><p>10.0080</p><p class="MsoNormal">text text text</p><p class="p--heading-2"><span class="anchor--on anchorname--160p001200">Schriftliche Bearbeitung</span</p><p>1.02</p><p>Eine blablabla text text</p><p>1.010</p><p>Ein text text (look <a xlink:type="simple" xlink:show="replace" xlink:role="17160" xlink:actuate="onRequest" xlink:href="link/a1000-text.xml">10.0060</a>) text text text</p> ..."

Now I want edit a part of string (c#) -> for example:

myText = myText.Replace("<p class="p--heading-2"><span class="anchor--on anchorname--160p00">Schriftliche Bearbeitung</span</p>", "<h2><a name="anchorname">Schriftliche Bearbeitung</a></p>");

The problem are the variable values (for excample the anchorname needs different values) and so I can´t replace the string.

Comment to first answer: I don´t want to use third-party supplier software (respective HtmlAgilityPack).

Are there any ideas for solution? If a regex the best solution, how the regex looks like? thanks.

Use HtmlAgilityPack not regex

var doc = new HtmlDocument();
doc.LoadHtml(html);

var nodes = doc.DocumentNode.SelectNodes("//p[@class='p--heading-2']");
foreach (HtmlNode htmlNode in nodes)
{
    var newNodeStr = htmlNode.InnerText;
    var newNode = HtmlNode.CreateNode("<h3><a>"+newNodeStr+"</a></h3>");
    htmlNode.ParentNode.ReplaceChild(newNode, htmlNode);
}

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