C# 的正規表達式 (Regular Expression)

基礎篇

C# 簡介

開發環境

變數與運算

流程控制

陣列

函數

物件

例外處理

函式庫篇

檔案處理

資料結構

正規表達式

Thread

應用篇

視窗程式

媒體影音

網路程式

遊戲程式

手機程式

資料庫

雲端運算

特殊功能

委派

擴展方法

序列化

LinQ

WPF

網路資源

教學影片

投影片

教學文章

軟體下載

考題解答

101習題

簡介

正規表達式是現代程式設計的重要工具,在 C# 當中,對正規表達式的支援相當的完整。您可以用正規表達式抽取出文件中的電話、地址、超連結、email 等欄位,因此正規表達式在文字型資料的處理上是相當方便的。

程式範例

在以下的範例中,我們利用正規表達式 "[0-9]+號",抽取出字串當中的號碼,像是 32號,45號等。其中的 matches 函數是正規表達式的主要部分,我們透過 Regex 物件中的 Match(pText) 與 NextMatch() 函數,不斷取得比對的結果 (Match m),然後再利用Match 結構取出 m.Groups[pGroupId].Value 這個比對的值,其中若 pGroudId 為 0 ,代表所要取得的是比對結果的全部。而 m.Success 可以用來判斷下一個比對是否成功,這可以做為回圈節數的條件。

using System;
using System.Collections;
using System.Text.RegularExpressions;
public class Regexp
{
 public static void Main(String[] args)
 {
 String text = "王小明:32號,李小華:45號";
 foreach (String s in matches("[0-9]+號", text, 0))
 Console.WriteLine(s);
 }
 public static IEnumerable matches(String pPattern, String pText, int pGroupId)
 {
 Regex r = new Regex(pPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
 for (Match m = r.Match(pText); m.Success; m = m.NextMatch())
 yield return m.Groups[pGroupId].Value;
 }
}

上述範例的執行結果如下所示,您可以看到字串 "王小明:32號,李小華:45號" 當中的 32號與 45 號被抽出來了,這正是正規表達式 "[0-9]+號" 所指定的樣式阿。

D:\myweb\teach\CSharpNetworkProgramming>csc RegexpTest1.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
D:\myweb\teach\CSharpNetworkProgramming>RegexpTest1
32號
45號

Post preview:

(will not be published)


本網頁的作者、授權與引用方式

作者
陳鍾誠,於金門大學資訊工程系,電子郵件:wt.ude.uqn|ccc#wt.ude.uqn|ccc,網站:http://ccckmit.wikidot.com
授權
本文採用創作共用 (Creative Common) 3.0 版的 姓名標示─非商業性─相同方式分享 授權條款,歡迎轉載或修改使用,但若做為商業使用時必須取得授權,引用本文時請參考下列格式。
中文版 (APA格式)
陳鍾誠 (10 Jun 2010 00:48),(網頁標題) C# 的正規表達式 (Regular Expression),(網站標題) 免費電子書:C# 程式設計,10 Jun 2010 00:48,取自 http://cs0.wikidot.com/regularexpression ,網頁修改第 3 版。
英文版 (APA格式)
Chung-Chen Chen (10 Jun 2010 00:48), Retrieved 10 Jun 2010 00:48 from http://cs0.wikidot.com/regularexpression, Page Revision 3.
page revision: 3, last edited: 01 Aug 2019 02:45
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /