[原創]Regex在c#中的學習和應用

來源:互聯網
上載者:User

DEELX Regex.chm [點擊下載]
通過學習Regex可以讓我們從大量的資訊中篩選出有用的部分。
通過Regex的學習我們可以簡化很多以前看起來無法實現的工作。
以下為我學習Regex的心得

學習篇
一。學習的首要條件是搭建好一個良好的學習環境,好的學習環境讓人學習事半功倍,在這裡我把我個人學習時搭建的環境提供出來。

後台cs檔案的代碼:

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

public partial class Default: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    /// <summary>
    /// Regex的調試環境
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_run_Click(object sender, EventArgs e)
    {
        string str_source = txt_content.Text;                               // 需要篩選的內容
        string str_reg = txt_reg.Text;                                          // 要匹配的Regex字串
        Regex reg = new Regex(str_reg);                        
        MatchCollection matchs = reg.Matches(str_source);            // 擷取符合匹配條件的集合
        // 將集合輸出顯示出來
        if (matchs.Count > 0)
        {
            for (int i = 0; i < matchs.Count; i++)
            {
                txt_output.Text += i + ":" + matchs[i].Value + "\r\n";
                GroupCollection groups = matchs[i].Groups;
                if (groups != null && groups.Count > 0)
                {
                    foreach (Group group in groups)
                    {
                        txt_output.Text += "\t@@ " + group.Value + "\r\n";
                    }
                }
            }
        }
        txt_msg.Text = matchs.Count + "";
    }

}


前台的aspx檔案的代碼:

Default.aspx

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

<!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 runat="server" ID="txt_content" Rows="5" Columns="100" TextMode="MultiLine" Wrap="true"></asp:TextBox>
        <br />
        Regex:<asp:TextBox runat="server" ID="txt_reg" Rows="5" Columns="100" TextMode="MultiLine" Wrap="true"></asp:TextBox>
        <br />
        <asp:Button runat="server" ID="btn_run" Text="測試Regex" OnClick="btn_run_Click" />
        匹配到:<asp:TextBox runat="server" ID="txt_msg" Width="40"></asp:TextBox>個
        <br />
        <asp:TextBox runat="server" ID="txt_output" Rows="20" Columns="100" TextMode="MultiLine" Wrap="true"></asp:TextBox>
    </div>
    </form>
</body>
</html>

顯示效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.