Asp.Net C# Html String Preview using Regex
I got开发者_运维百科 a string preview class which takes a Html string from the database or just plain old html string and outputs a preview of x characters.... Now my boss asked me to convert it into regex, and I been striking a wall for a while now. If anyone can help me with that.
The specific part that mostly concerns me is getting x characters without including tags in the count but not killing the tags either.
I would love if anyone has anything i read on or a codeplex thing.
The task is simple my friend... sounds like an interesting boss.
void Main()
{
string test = "<html>wowzers description: none <div>description:a1fj391</div></html>";
string result = getFirstChars(test, 15);
Console.WriteLine(result);
//result: wowzers descrip
}
static Regex MyRegex = new Regex(
"(?<tag></?\\s*\\w+\\s*>*)",
RegexOptions.Compiled);
static string getFirstChars(string html, int count)
{
string nonTagText = MyRegex.Replace(html,"");
return nonTagText.Substring(0, count);
}
if you want to keep tags... then you could do this:
void Main()
{
string test = "<html><b>wowzers</b> description: none <div>description:a1fj391</div></html>";
string result = getFirstChars(test, 15);
Console.WriteLine(result);
//result: <html><b>wowzers</b> descrip
}
static Regex MyRegex = new Regex(
"(?<tag></?\\s*\\w+\\s*>)(?<content>[^<]*)",
RegexOptions.Compiled);
static string getFirstChars(string html, int count)
{
int totalCount = 0;
int contentCount = 0;
foreach(Match match in MyRegex.Matches(html))
{
contentCount += match.Groups["content"].Length;
totalCount += match.Length;
if(contentCount >= count)
{
totalCount -= contentCount - count;
break;
}
}
return html.Substring(0, totalCount);
}
精彩评论