利用正規表達式(Regular Expression)將html內容的tag標籤刪除掉

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegularExpression.aspx.cs"
ValidateRequest="false"
Inherits="SK_RegularExpression" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox1" runat="server"  TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="height: 21px" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class SK_RegularExpression : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String ZipRegex = @"<\s*(?<Tag>/*\s*[^<^>^\s]+)(?<Para>(\s*[^<^>^\s]+)*)\s*>";
        String newText = Regex.Replace(this.TextBox1.Text, ZipRegex, "");
        this.Label1.Text = newText;

    }
}

 

教學

http://www.javaworld.com.tw/jute/post/view?bid=20&id=130126&sty=1&tpg=1&age=0

arrow
arrow
    全站熱搜

    sophieProfession 發表在 痞客邦 留言(0) 人氣()