去掉 HTML 标签?

发布于 2024-12-10 07:09:11 字数 793 浏览 0 评论 0原文

如何剥离此文本,

<html>

<body>      

<h1>My First Heading</h1>

<p>My first paragraph.</p>
<[email protected]>
</body>
</html>

使其看起来像

My First Heading
My first paragraph.
<[email protected]>

功能

public static string StripHTML(this string htmlText)
    {
        var reg = new Regex("<(.|\n)*?>", RegexOptions.IgnoreCase);
        return reg.Replace(htmlText, "");
    }

使用“我得到

我的第一个标题” 我的第一段。

How to strip this text

<html>

<body>      

<h1>My First Heading</h1>

<p>My first paragraph.</p>
<[email protected]>
</body>
</html>

to look like

My First Heading
My first paragraph.
<[email protected]>

Using the function

public static string StripHTML(this string htmlText)
    {
        var reg = new Regex("<(.|\n)*?>", RegexOptions.IgnoreCase);
        return reg.Replace(htmlText, "");
    }

I get

My First Heading
My first paragraph.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-12-17 07:09:11

使用 Html Agility Pack 进行此类操作。它比任何正则表达式都快并且支持 LINQ。

Use Html Agility Pack for these kinds of operations. It is faster than any regex and supports LINQ.

一抹淡然 2024-12-17 07:09:11
static void Main(string[] args)
    {


      string modified_html =  emas(input);

        HtmlDocument doc = new HtmlDocument();

        doc.LoadHtml(modified_html);

        string test1 = doc.DocumentNode.InnerText;


        Console.WriteLine();


        var reg = new Regex("<(.|\n)*?>", RegexOptions.IgnoreCase);

        Console.WriteLine(reg.Replace(modified_html , ""));

        Console.Read();
    }


    public static string emas(string text)
    {

        string stripped = text;

        const string MatchEmailPattern =
       @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
       + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
         + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
        Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        // Find matches.
        MatchCollection matches = rx.Matches(text);
        // Report the number of matches found.
        int noOfMatches = matches.Count;
        // Report on each match.
        foreach (Match match in matches)
        {

            stripped = stripped.Replace("<"+ match.Value + ">" , match.Value);

        }


        return stripped;


    }



   static string input = " Your html goes here  ";
static void Main(string[] args)
    {


      string modified_html =  emas(input);

        HtmlDocument doc = new HtmlDocument();

        doc.LoadHtml(modified_html);

        string test1 = doc.DocumentNode.InnerText;


        Console.WriteLine();


        var reg = new Regex("<(.|\n)*?>", RegexOptions.IgnoreCase);

        Console.WriteLine(reg.Replace(modified_html , ""));

        Console.Read();
    }


    public static string emas(string text)
    {

        string stripped = text;

        const string MatchEmailPattern =
       @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
       + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
         + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
        Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        // Find matches.
        MatchCollection matches = rx.Matches(text);
        // Report the number of matches found.
        int noOfMatches = matches.Count;
        // Report on each match.
        foreach (Match match in matches)
        {

            stripped = stripped.Replace("<"+ match.Value + ">" , match.Value);

        }


        return stripped;


    }



   static string input = " Your html goes here  ";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文