Get html tags from textbox in C#
I have a text area in my ASP.NET web application that is used to enter html code. Now there is also an button control, which upon clicking should retrieve just the text placed between certain html tags in the text box.
For example:
1) User types html code including tags etc. and clicks the OK button
2) In my code the text from the text area is retrieved and only the part between a <p></p>
tag should be saved to a string object.
I can obviously get the text from the text area and attach it to a string object but I am not able to work out how to 开发者_开发百科just get text within a certain html tag like <p></p>
. Could someone help me out please?
Try this... example taken from MSDN and amended slightly 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();
}
}
}
You can see this in action at ideone.com.
If you want to include your <p>
tags in the result, then just change where you put the brackets in the regular expression to this:
string pat = @"(<p>(?:.|\r|\n)+?</p>)";
精彩评论