正規表示式

2023-01-18 18:36:03 字數 3633 閱讀 3693

c#正規表示式經典分類整理集合手冊

作者: **: 發布日期:2009-03-27

有一段時間,正規表示式學習很火熱很潮流,當時在csdn一天就能看到好幾個正規表示式的帖子,那段時間借助論壇以及wrox press出版的《c#字串和正規表示式參考手冊》學習了一些基礎的知識,同時也為我在csdn大概賺了1000分,今天想起來,去找《c#字串和正規表示式參考手冊》時,已經不知所蹤了。現在用到正則的時候也比較少,把以前的筆記等整理一下,以誌不忘。

(1)「@」符號

符下兩ows表研究室的火熱,當晨在「@」雖然並非c#正規表示式的「成員」,但是它經常與c#正規表示式出雙入對。「@」表示,跟在它後面的字串是個「逐字字串」,不是很好理解,舉個例子,以下兩個宣告是等效的:

string x="d:\\my huang\\my doc";

string y = @"d:\my huang\my doc";

事實上,如果按如下宣告,c#將會報錯,因為「\」在c#中用於實現轉義,如「\n」換行:

string x = "d:\my huang\my doc";

(2)基本的語法字元。

\d 0-9的數字

\d \d的補集(以所以字元為全集,下同),即所有非數字的字元

\w 單詞字元,指大小寫字母、0-9的數字、下劃線

\w \w的補集

\s 空白字元,包括換行符\n、回車符\r、製表符\t、垂直製表符\v、換頁符\f

\s \s的補集

. 除換行符\n外的任意字元

[…] 匹配內所列出的所有字元

[^…] 匹配非內所列出的字元

下面提供一些簡單的示例:view plaincopy to clipboardprint?

string i = "\n";

string m = "3";

regex r = new regex(@"\d");

//同regex r = new regex("\\d");

結果:true

結果:false

string i = "%";

string m = "3";

regex r = new regex("[a-z0-9]");

//匹配小寫字母或數字字元

結果:false

結果:true

string i = "\n";

string m = "3";

regex r = new regex(@"\d");

//同regex r = new regex("\\d");

結果:true

結果:false

string i = "%";

string m = "3";

regex r = new regex("[a-z0-9]");

//匹配小寫字母或數字字元

結果:false

結果:true

(3)定位字元

「定位字元」所代表的是乙個虛的字元,它代表乙個位置,你也可以直觀地認為「定位字元」所代表的是某個字元與字元間的那個微小間隙。

^ 表示其後的字元必須位於字串的開始處

$ 表示其前面的字元必須位於字串的結束處

\b 匹配乙個單詞的邊界

\b 匹配乙個非單詞的邊界

另外,還包括:\a 前面的字元必須位於字元處的開始處,\z 前面的字元必須位於字串的結束處,\z 前面的字元必須位於字串的結束處,或者位於換行符前

下面提供一些簡單的示例: view plaincopy to clipboardprint?

string i = "live for nothing,die for something";

regex r1 = new regex("^live for nothing,die for something$");

true

regex r2 = new regex("^live for nothing,die for some$");

false

regex r3 = new regex("^live for nothing,die for some");

true

string i = @"live for nothing,

die for something";//多行

regex r1 = new regex("^live for nothing,die for something$");

"r1 match count:" +

regex r2 = new regex("^live for nothing,die for something$",

"r2 match count:" +

regex r3 = new regex("^live for nothing,\r\ndie for something$");

"r3 match count:" +

regex r4 = new regex("^live for nothing,$");

"r4 match count:" +

regex r5 = new regex("^live for nothing,$",

"r5 match count:" +

regex r6 = new regex("^live for nothing,\r\n$");

"r6 match count:" +

regex r7 = new regex("^live for nothing,\r\n$",

"r7 match count:" +

regex r8 = new regex("^live for nothing,\r$");

"r8 match count:" +

regex r9 = new regex("^live for nothing,\r$",

"r9 match count:" +

regex r10 = new regex("^die for something$");

"r10 match count:" +

regex r11 = new regex("^die for something$",

"r11 match count:" +

regex r12 = new regex("^");

"r12 match count:" +

regex r13 = new regex("$");

"r13 match count:" +

regex r14 = new regex("^",

"r14 match count:" +

regex r15 = new regex("$",

"r15 match count:" +

regex r16 = new regex("^live for nothing,\r$\n^die for something$",

"r16 match count:" +

//對於乙個多行字串,在設定了multiline選項之後,^和$將出現多次匹配。

string i = "live for nothing,die for something";

string m = "live for nothing,die for some thing";

regex r1 = new regex(@"\bthing\b");

"r1 match count:" +

常用正規表示式

漢字 u4e00 u9fff 考證數字 0 9 考證n位的數字 d 考證至多n位數字 d 考證m n位的數字 d 考證零和非零開頭的數字 0 1 9 0 9 考證有兩位小數的正實數 0 9 0 9 考證有1 3位小數的正實數 0 9 0 9 考證非零的正整數 1 9 0 9 考證非零的負整數 1 9...

正規表示式詳細

正規表示式是一種文字模式,包括普通字元 例如,a 到 z 之間的字母 和特殊字元 稱為 元字元 模式描述在搜尋文字時要匹配的乙個或多個字串。下面是正規表示式的一些示例 表示式匹配 s 匹配空行。d d 驗證由兩位數字 乙個連字元再加 5 位數字組成的 id 號。s s s s s s 1 s 匹配 ...

grep 命令 正規表示式

unix grep的用法 grep 全域性正規表示式版本 允許對文字檔案進行模式查詢,grep支援基本正規表示式,也支援其擴充套件集。grep有三種變形 grep標準grep命令 egrep擴充套件grep命令,支援基本及擴充套件的正規表示式,但不支援 q模式範圍的應用 fgrep快速grep命令,...