Get html tags from text field in c #

I have a text area in my ASP.NET web application that is used to enter html code. Now there is a control button that, after clicking, should extract only the text placed between certain html tags in the text field.

For instance:

1) The user type of the html code, including tags, etc., and clicks the OK button 2) In my code, text is extracted from the text area, and only the part between the <p></p> should be saved to a string object.

I obviously can get the text from the text area and attach it to the string object, but I can't figure out how easy it is to get the text in a specific html tag, like <p></p> . Can someone help me please?

+4
source share
1 answer

Try this ... example taken from MSDN and slightly modified to show your situation:

 using System; using System.Text.RegularExpressions; class Example { static void Main() { string text = "start <p>I want to capture this</p> end"; string pat = @""<p>((?:.|\r|\n)+?)</p>""; // Instantiate the regular expression object. Regex r = new Regex(pat, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. Match m = r.Match(text); int matchCount = 0; while (m.Success) { Console.WriteLine("Match"+ (++matchCount)); for (int i = 1; i <= 2; i++) { Group g = m.Groups[i]; Console.WriteLine("Group"+i+"='" + g + "'"); CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count; j++) { Capture c = cc[j]; System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index); } } m = m.NextMatch(); } } } 

This can be seen in action ideone.com .

If you want to include <p> tags in the result, just change where you put the brackets in the regular expression:

 string pat = @"(<p>(?:.|\r|\n)+?</p>)"; 
+2
source

All Articles