开发者

Get seleted text parent tag using regex C#

开发者 https://www.devze.com 2022-12-18 11:58 出处:网络
<SPAN id=spanD121C150D2 style=\"BACKGROUND-COLOR: antiquewhite\" CategoryID=\"1\" MessageID=\"2316\" refSpan=\"\">
 <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?


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.


this this possible using regex ? p

No. Use an HTML parser.


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;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号