//Regex.IsMatch();//用來判斷給定的字符串是否匹配某個正則表達式 //Regex.Match();//用來從給定的字符串中按照正則表達式的要求提取【一個】匹配的字符串 //Regex.Matches();//用來從給定的字符串中按照正則表達式的要求提取【所有】匹配的字符串 //Regex.Replace(); //替換所有正則表達式匹配的字符串為另外一個字符串。
有關元字符 請看這篇文章: http://www.cnblogs.com/entclark/p/7802060.html
小練習:
1.請用戶輸入一個10(含)-25(含)之間的任何一個數字 正則表達式:"^(1[0-9]|2[0-5])$" 2.驗證是否是合法的手機號 正則表達式:@"^\d{11}$" 3.身份證號碼 //1.長度為15位或者18位的字符串,首位不能是0。 //2.如果是15位,則全部是數字。 //3.如果是18位,則前17位都是數字,末位可能是數字也可能是X。 正則表達式:"^[1-9]\d{16}[0-9xX]|[1-9]\d{14}$", //[num or other],數字或者其它字符都可以,表示其中的一個; 4.驗證是否為合法的郵件地址 正則表達式: @"^[-0-9a-zA-Z_\.]+@[a-zA-Z0-9]+(\.[a-zA-Z]+){1,2}$" //出現多個,碰到指定字符就截止,最好是[xxx]+@;不用(xxx)+@ 5.匹配IP地址,4段用.分割的最多三位數字 正則表達式:@"^([0-9]{1,3}\.){3}[0-9]{1,3}$" 6.判斷是否是合法的日期格式“2008-08-08”。四位數字-兩位數字-兩位數字 正則表達式: @"^[0-9]{4}-[0-9]{2}-[0-9]{2}$ 7.判斷是否是合法的url地址 正則表達式:@"^[a-zA-Z0-9]+://.+$
字符串提取:(一般字符串提取不加^ 和 $,不限制)
1.Regex.Match只能提取一個匹配,結果是2010 string msg = "大家好呀,hello,2010年10月10日是個好日子。恩,9494.吼吼!886."; Match match = Regex.Match(msg, "[0-9]+"); 2.Regex.Matches()提取字符串中的所有匹配 MatchCollection matches = Regex.Matches(msg, "[0-9]+"); //結果是2010,10,10,9494,886 3.組, 在正則表達式中只要出現了()就表示進行了分組。小括號既有改變優先級的作用又具有提取組的功能。 string html = File.ReadAllText("regex.htm"); MatchCollection matches = Regex.Matches(html, @"([-a-zA-Z_0-9.]+)@([-a-zA-Z0-9_]+(\.[a-zA-Z]+)+)"); foreach (Match item in matches) { //item.Value表示本次提取到的字符串 //item.Groups集合中存儲的就是所有的分組信息。 //item.Groups[0].Value與item.Value是等價的都表示本次提取到的完整的字符串,表示整個郵箱字符串,而item.Groups[1].Value則表示第一組的字符串。 //Console.WriteLine(item.Value); Console.WriteLine("第0組:{0}", item.Groups[0].Value); Console.WriteLine("第1組:{0}", item.Groups[1].Value); Console.WriteLine("第2組:{0}", item.Groups[2].Value); Console.WriteLine("==============================================="); } Console.WriteLine(matches.Count); 4.從“June26 , 1951 ”中提取出月份June、26、1951來。 解析:月份和日之間是必須要有空格分割的,所以使用空白符號“\s”匹配所有的空白字符,此處的空格是必須有的,所以使用“+”標識為匹配1至多個空格。之后的“,”與年份之間的空格是可有可無的,所以使用“*”表示為匹配0至多個 string date = "June26 , 1951 "; Match match = Regex.Match(date, @"([a-zA-Z]+)\s*([0-9]{2})\s*,\s*([0-9]{4})\s*"); for (int i = 0; i < match.Groups.Count; i++) { Console.WriteLine(match.Groups[i].Value); } Console.ReadKey(); 5.從Email中提取出用戶名和域名,比如從test@163.com中提取出test和163.com。 Console.WriteLine("請輸入Email:"); string email = Console.ReadLine(); Match match = Regex.Match(email, @"(.+)@(.+)"); Console.WriteLine("用戶名:{0},域名:{1}", match.Groups[1].Value, match.Groups[2].Value); 6.192.168.10.5[port=21,type=ftp]”,這個字符串表示IP地址為192.168.10.5的服務器的21端口提供的是ftp服務,其中如果“,type=ftp”部分被省略,則默認為http服務。請用程序解析此字符串,然后打印出“IP地址為***的服務器的***端口提供的服務為*** // string msg = "192.168.10.5[port=21,type=ftp]"; string msg = "192.168.10.5[port=21]"; Match match = Regex.Match(msg, @"(.+)\[port=([0-9]{2,5})(,type=(.+))?\]"); Console.WriteLine("ip:{0}", match.Groups[1].Value); Console.WriteLine("port:{0}", match.Groups[2].Value); Console.WriteLine("services:{0}", match.Groups[4].Value.Length == 0 ? "http" : match.Groups[4].Value); Console.ReadKey();
反向引用
①使用$n替換正則中的第n組
//將hello ‘welcome’ to ‘China’ 替換成 hello 【welcome】 to 【China】
string msg = "hello 'welcome' to 'China' 'lss' 'ls' 'szj' ";
msg = Regex.Replace(msg, "'(.+?)'", "【$1】$$1");//2個$$代表一個,轉義了
Console.WriteLine(msg);
Console.ReadKey();
//===================================
//隱藏手機號碼
string msg = "xxx13409876543yyy18276354908aa87654321345bb98761234654";
msg = Regex.Replace(msg, @"([0-9]{3})[0-9]{4}([0-9]{4})", "$1****$2");
Console.WriteLine(msg);
//======隱藏郵箱名=====================
② 使用\n,表示引用第n組的內容,下面例子:(.)\1+,表示(.)這個組的內容,如X,\1引用了這組,+表示引用這組內容至少出現1次,所以匹配到的就是XXXXXYYYYYYZZZZZZZ ,$1替換成第一組的內容X;
string msg = "你們喜歡XXXXXYYYYYYZZZZZZZ";
msg = Regex.Replace(msg, @"(.)\1+", "$1");//Replace最后一個參數還可以對我們輸入的正則進行操作
Console.WriteLine(msg);//你們喜歡zyz
Console.ReadKey();
③(.)出現了一次,接着\1,對組的內容又出現一次,所以就能匹配AA string msg = "AABB"; MatchCollection matches = Regex.Matches(msg, @"(.)\1(.)\2"); foreach (Match item in matches) { Console.WriteLine(item.Value); } ④將一段文本中的MM/DD/YYYY格式的日期轉換為YYYY-MM-DD格式 ,比如“我的生日是05/21/2010耶”轉換為“我的生日是2010-05-21耶”。 string msg = "我的生日是05/21/2010耶我的生日是05/21/2010耶"; msg = Regex.Replace(msg, @"(\d{2})/(\d{2})/(\d{4})", "$3-$1-$2", RegexOptions.ECMAScript); Console.WriteLine(msg); ⑤ 給一段文本中匹配到的url添加超鏈接,比如把http://www.test.com替換為<a href="http://www.test.com"> http://www.test.com</a>。因為這個是整體做為一個組,比較特殊. string msg = "給一段文本中匹配到的url添加超鏈接,比如把http://www.test.com替換http://www.sina.com.cn哈哈http://www.google.com"; msg = Regex.Replace(msg, @"[a-zA-Z0-9]+://[-a-zA-Z0-9.?&=#%\/_]+", "<a href=\"$0\">$0</a>"); Console.WriteLine(msg); Console.ReadKey();
單詞邊界
A: string msg = "The day after tomorrow is my wedding day.The row we are looking for is .row. number 10."; //\b :表示單詞的邊界。 //什么叫做“單詞”? [a-zA-Z0-9_] msg = Regex.Replace(msg, @"\brow\b", "line"); Console.WriteLine(msg); Console.ReadKey(); B: //請提取出3個字母的單詞。 string msg = "Hi,how are you?Welcome to our country!"; MatchCollection matches = Regex.Matches(msg, @"\b[a-z]{3}\b", RegexOptions.IgnoreCase); foreach (Match item in matches) { Console.WriteLine(item.Value); } Console.ReadKey();