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

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

這個範例是最近剛好有小舖的網友所提出來的問題....
主要是透過正規表達式找到是<>或< />的html tag都給濾除掉
c#範例
regular_CS.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="regular_CS.aspx.cs" Inherits="regular_CSspx"
02     ValidateRequest="false" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07     <title>未命名頁面</title>
08 </head>
09 <body>
10     <form id="form1" runat="server">
11         <div>
12             <asp:TextBox ID="TextBox1" runat="server" Height="131px" TextMode="MultiLine" Width="268px"></asp:TextBox>
13             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="處理" /><br />
14             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
15     </form>
16 </body>
17 </html>
18


regular_CS.aspx.cs

01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Text.RegularExpressions;

12
13 public partial class regular_CSspx : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17     }

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

26 }