简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM