Reputation: 5095
<SPAN id=spanD121C150D2 style="BACKGROUND-COLOR: antiquewhite" CategoryID="1" MessageID="2316" refSpan="">
<SPAN id=span1CE69EDE12 style="BACKGROUND-COLOR: blue" CategoryID="2" MessageID="2316" refSpan="">platnosci inny srodkiem platnosci. DC - zakup paliwa na stacji benzynowej 101-500 (150 zl). 27
</SPAN>
</SPAN>
I have a string like above.
If the selected text is "srodkiem ", is it possible to get the relevant span tag?
Is this possible using a regular expression?
Upvotes: 1
Views: 1129
Reputation: 5095
I wrote a small function with HTML Agility pack:
public static HtmlNode GetNodeByInnerText(string sHTML, string sText)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sHTML);
int Position = sHTML.ToString().IndexOf(sText);
return FindNodeByPosition(doc.DocumentNode, Position);
}
private static HtmlNode FindNodeByPosition(HtmlNode node, int Position)
{
HtmlNode foundNode = null;
foreach (HtmlNode child in node.ChildNodes)
{
if (child.StreamPosition <= Position &
((child.NextSibling == null) || child.NextSibling.StreamPosition > Position))
{
if (child.ChildNodes.Count > 0 &
!(child.ChildNodes.Count == 1 && child.InnerHtml == child.FirstChild.InnerHtml))
{
foundNode = FindNodeByPosition(child, Position);
break; // TODO: might not be correct. Was : Exit For
}
else
{
return child;
}
}
}
return foundNode;
}
Upvotes: 0
Reputation: 57976
Yes, it's possible with regex, but you should to use Html Agility Pack. Using that will be much less painful to maintain your code.
Upvotes: 1
Reputation: 545985
this this possible using regex ? p
No. Use an HTML parser.
Upvotes: 0