国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Regex > 正文

C#正則表達式Regex常用匹配

2020-03-16 21:00:21
字體:
來源:轉載
供稿:網友

使用Regex類需要引用命名空間:using System.Text.RegularExPRessions;

利用Regex類實現驗證

示例1:注釋的代碼所起的作用是相同的,不過一個是靜態(tài)方法,一個是實例方法

var source = "劉備關羽張飛孫權何問起";//Regex regex = new Regex("孫權");//if (regex.IsMatch(source))//{// Console.WriteLine("字符串中包含有敏感詞:孫權!");//}if (Regex.IsMatch(source, "孫權")) {  Console.WriteLine("字符串中包含有敏感詞:孫權!");}Console.ReadLine();

 

示例2:使用帶兩個參數的構造函數,第二個參數指示忽略大小寫,很常用

var source = "123abc345DEf";Regex regex = new Regex("def",RegexOptions.IgnoreCase);if (regex.IsMatch(source)){  Console.WriteLine("字符串中包含有敏感詞:def!");} Console.ReadLine();

 

 

使用Regex類進行替換

示例1:簡單情況

var source = "123abc456ABC789";// 靜態(tài)方法//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);// 實例方法Regex regex = new Regex("abc", RegexOptions.IgnoreCase);var newSource = regex.Replace(source, "|");Console.WriteLine("原字符串:"+source);Console.WriteLine("替換后的字符串:" + newSource);Console.ReadLine();

 

結果:

原字符串:123abc456ABC789

替換后的字符串:123|456|789

 

示例2:將匹配到的選項替換為html代碼,我們使用了MatchEvaluator委托

var source = "123abc456ABCD789"; Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);var newSource = regex.Replace(source,new MatchEvaluator(OutPutMatch));Console.WriteLine("原字符串:"+source);Console.WriteLine("替換后的字符串:" + newSource);Console.ReadLine(); //柔城private static string OutPutMatch(Match match){  return "<b>" +match.Value+ "</b>";}

 

輸出:

原字符串:123abc456ABCD789

替換后的字符串:123<b>abc</b>456<b>ABC</b>D789

 

C#正則表達式Regex常用匹配

在線測試:http://tool.hovertree.com/a/zz/

 1             #region 身份證號碼正則表達式 2             //何問起 3              4                 Console.WriteLine("請輸入一個身份證號碼"); 5                 string id = Console.ReadLine(); 6                 bool b4 = Regex.IsMatch(id, @"^/d{15}|/d{18}$"); 7                 bool b5 = Regex.IsMatch(id, @"^(/d{15}|/d{18})$"); 8                 Console.WriteLine(b4); 9                 Console.WriteLine(b5);10             11             #endregion12 13             #region 匹配電話號碼14             //hovertree15             16 17                 Console.WriteLine("請輸入電話號碼");18                 string phone = Console.ReadLine();19                 bool b = Regex.IsMatch(phone, @"^((/d{3,4}/-/d?{7,8})|(/d{5}))$");20                 Console.WriteLine(b);21             22             #endregion23 24             #region 匹配email的regex25 26             //hovertree27             28                 Console.WriteLine("請輸入Email地址");29                 string email = Console.ReadLine();30                 bool bhvt = Regex.IsMatch(email, @"^/w+@/w+/./w+$");31                 Console.WriteLine(bhvt);32             33             #endregion34 35             #region 匹配ip地址的regex36             //hovertree37             38                 Console.WriteLine("請輸入一個IP地址");39                 string ip = Console.ReadLine();40                 bool bkly = Regex.IsMatch(ip, @"^/d{1,3}(/./d{1,3}){3}$");41                 Console.WriteLine(bkly);42             43             #endregion44 45             #region 匹配日期合法regex46             //何問起47             48                 Console.WriteLine("請輸入一個日期");49                 string date = Console.ReadLine();50                 bool bhovertree = Regex.IsMatch(date, @"^/d{4}/-/d{1,2}/-/d{1,2}$");51                 Console.WriteLine(bhovertree);52             53             #endregion54 55 56             #region 匹配url地址的regex57             //"http://hovertree.com"58             //"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"59             //"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"60             //"ftp://127.0.0.1/myslider.txt"61 62             //hovertree63             64                 Console.WriteLine("請輸入url地址");65                 string url = Console.ReadLine();66                 bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");67                 Console.WriteLine(bkeleyi);68             69             #endregion

asp.net開源CMS http://www.cnblogs.com/sosoft/p/cms.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤凰县| 都江堰市| 威海市| 乐陵市| 巴楚县| 安阳县| 陕西省| 盐津县| 临西县| 苏尼特左旗| 万荣县| 宣恩县| 乡城县| 耒阳市| 乌拉特后旗| 沙雅县| 宜宾市| 梁平县| 无锡市| 龙陵县| 秦皇岛市| 罗定市| 醴陵市| 永川市| 绥德县| 武清区| 讷河市| 永康市| 东安县| 稷山县| 江孜县| 虞城县| 焦作市| 伊宁县| 南投县| 普格县| 永顺县| 武鸣县| 昔阳县| 青海省| 威信县|