tags:

views:

288

answers:

3
 <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?

+1  A: 

this this possible using regex ? p

No. Use an HTML parser.

Konrad Rudolph
+1  A: 

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.

Rubens Farias
hello, thank your for your prompt response. can u give me a sample how to find "srodkiem" inside a given string a get the parent element ?Thanks
kakopappa
A: 

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