羅馬數字
string p1 = "^m*(d?c|c[dm])" + "(l?x|x[lc])(v?i|i[vx])$";
string t1 = "vii";
match m1 = regex.match(t1, p1);
交換前二個單詞
string t2 = "the quick brown fox";
string p2 = @"(\s+)(\s+)(\s+)";
regex x2 = new regex(p2);
string r2 = x2.replace(t2, "$3$2$1", 1);
關健字=值
string t3 = "myval = 3";
string p3 = @"(\w+)\s*=\s*(.*)\s*$";
match m3 = regex.match(t3, p3);
實現每行80個字元
string t4 = "********************"
+ "******************************"
+ "******************************";
string p4 = ".";
match m4 = regex.match(t4, p4);
月/日/年 小時:分:秒的時間格式
string t5 = "01/01/01 16:10:01";
string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)";
match m5 = regex.match(t5, p5);
改變目錄(僅適用於windows平台)
string t6 = @"c:\documents and settings\user1\desktop\";
string r6 = regex.replace(t6,@"\\user1\\", @"\\user2\\");
擴充套件16位轉義符
string t7 = "%41"; // capital a
string p7 = "%([0-9a-fa-f][0-9a-fa-f])";
string r7 = regex.replace(t7, p7, hexconvert);
刪除字串中開始和結束處的空格
string t9a = " leading";
string p9a = @"^\s+";
string r9a = regex.replace(t9a, p9a, "");
string t9b = "trailing ";
string p9b = @"\s+$";
string r9b = regex.replace(t9b, p9b, "");
在字元\後新增字元n,使之成為真正的新行
string t10 = @"\ntest\n";
string r10 = regex.replace(t10, @"\\n", "\n");
轉換ip位址
string t11 = "55.54.53.52";
string p11 = "^" +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])" +
"$";
match m11 = regex.match(t11, p11);
刪除檔名包含的路徑
string t12 = @"c:\file.txt";
string p12 = @"^.*\\";
string r12 = regex.replace(t12, p12, "");
聯接多行字串中的行
string t13 = @"this is
a split line";
string p13 = @"\s*\r?\n\s*";
string r13 = regex.replace(t13, p13, " ");
提取字串中的所有數字
string t14 = @"
test 1
test 2.3
test 47
"; string p14 = @"(\d+\.?\d*|\.\d+)";
matchcollection mc14 = regex.matches(t14, p14);
找出所有的大寫字母
string t15 = "this is a test of all caps";
string p15 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc15 = regex.matches(t15, p15);
找出小寫的單詞
string t16 = "this is a test of lowercase";
string p16 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc16 = regex.matches(t16, p16);
找出第乙個字母為大寫的單詞
string t17 = "this is a test of initial caps";
string p17 = @"(\b[^\wa-z0-9_][^\wa-z0-9_]*\b)";
matchcollection mc17 = regex.matches(t17, p17);
找出簡單的html語言中的鏈結
string t18 = @"
first tag text
next tag text
"; string p18 = @"]*?href\s*=\s*[""']?" + @"([^'"" >]+?)[ '""]?>";
matchcollection mc18 = regex.matches(t18, p18, "si");
正規表示式 常用正規表示式
一 校驗數字的表示式 1 數字 0 9 2 n位的數字 d 3 至少n位的數字 d 4 m n位的數字 d 5 零和非零開頭的數字 0 1 9 0 9 6 非零開頭的最多帶兩位小數的數字 1 9 0 9 0 9 7 帶1 2位小數的正數或負數 d d 8 正數 負數 和小數 d d 9 有兩位小數的...
正規表示式 常用正規表示式
網域名稱 a za z0 9 a za z0 9 a za z0 9 a za z0 9 interneturl a za z s 或 http w w w 手機號碼 13 0 9 14 5 7 15 0 1 2 3 4 5 6 7 8 9 18 0 1 2 3 5 6 7 8 9 d 或者 1 3...
正規表示式常用
正規表示式的介紹 1 資料型別 基本資料型別 number string boolean null undefined 複雜資料型別 array function object math date regexp正規表示式 string number boolean 2 regular express...