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

首頁 > 編程 > C# > 正文

輕松學習C#的正則表達式

2020-01-24 01:22:16
字體:
來源:轉載
供稿:網友

      在編寫處理字符串的程序時,經常會有查找符合某些復雜規則的字符串的需要。正則表達式就是用于描述這些規則的工具。正則表達式擁有一套自己的語法規則,常見語法包括字符匹配,重復匹配,字符定位,轉義匹配和其他高級語法(字符分組,字符替換和字符決策),使用正則表達式時,首先構造正則表達式,這就用到了Regex類。其構造方式有兩種:
        基本形式Regex(正則表達式)
        基本形式Regex(正則表達式,匹配選項)
其中匹配選項是提供一些特殊的幫助,是一個枚舉值,包括下面六個值:

  •         (1)IgnoreCase(忽略大小寫)
  •         (2)ReghtToLeft(從左向右)
  •         (3)None(默認)
  •         (4)CultureInvariant(忽略區域)
  •         (5)Multline(多行模式)
  •         (6)SingleLine(單行模式)

一、正則表達式的匹配
        正則表達式的匹配是通過Regex類的IsMatch方法實現的,IsMatch方法的使用格式為:
        Regex.IsMatch(要判斷的字符串,正則表達式)
例一:利用IsMatch方法判斷是否符合當地的電話號碼的程序

<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;//引入命名空間 using System.Threading.Tasks;  namespace 正則表達式 {  class Program  {  static void Main(string[] args)  {  string str = @"(0530|0530-)/d{7,8}";//定義的正則表達式符合條件為“0530”或  Console.WriteLine("請輸入一個電話號碼");//“0530-”開頭,后面跟7位或8位數字  string tel = Console.ReadLine();  bool b;  b = Regex.IsMatch(tel,str);//判斷是否符合正則表達式  if (b)  {  Console.WriteLine("{0}是某地的電話號碼",tel);  }  else  {  Console.WriteLine("{0}不是某地的電話號碼",tel);  }  Console.ReadLine();  }  } }</span> 

輸入:0530-12345678
輸出的結果為:0530-12345678是某地的電話號碼
二、正則表達式的替換
        要通過正則表達式替換字符串中的匹配項,就要通過Regex類中的Replace方法。通常用的一種格式為:
        Regex.Replace(要搜索匹配項的字符串,要替換的原字符串,替換后的字符串)
例二:利用Replace方法將用戶輸入的電子郵件中的“@”替換為“AT”的程序

<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;  namespace ConsoleApplication2 {  class Program  {  static void Main(string[] args)  {  string str = @"/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";//定義的電子郵件地址的正則表達式  Console.WriteLine("請輸入一個正確的Internet電子郵件地址");  string email = Console.ReadLine();  bool b;  b = Regex.IsMatch(email, str);//判斷是否符合正則表達式  if (b)  {  string outstr = "";  outstr = Regex.Replace(email, "@", "AT");//進行替換  Console.WriteLine("替換后為:{0}", outstr);  }  else  {  Console.WriteLine("你所輸入的字符串中不包括Internet URL");  }  Console.ReadLine();  }  } }</span> 

輸入:123456@126.com
輸出的結果為:123456AT126.com
三、正則表達式的拆分
           要通過正則表達式拆分字符串,就要通過Regex類的Split方法,格式為:
           Regex.Split(要拆分的字符串,要匹配的正則表達式模式)
例三:通過Split方法進行輸入的字符串的拆分

<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;  namespace ConsoleApplication2 {  class Program  {  static void Main(string[] args)  {  string str = ";";//定義的正則表達式  Console.WriteLine("請輸入多個用戶姓名,以分號隔開");  string names = Console.ReadLine();  string[] name;  name = Regex.Split(names,str);  Console.WriteLine("分隔后的姓名為:");  foreach (string item in name)  {  Console.WriteLine(item);  }  Console.ReadLine();  }  } }</span> 

輸入:張三;李四;王五
輸出的結果為:張三
                       李四
                       王五

以上就是C#的正則表達式,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涞源县| 滕州市| 临清市| 浦江县| 泸州市| 昭平县| 桦南县| 宝兴县| 三河市| 铜山县| 三门峡市| 银川市| 开鲁县| 宁波市| 南召县| 新巴尔虎右旗| 化州市| 浮山县| 长葛市| 漠河县| 常德市| 丰县| 三门县| 永仁县| 凤冈县| 新化县| 兴安县| 宜川县| 西藏| 杭锦后旗| 南岸区| 建阳市| 屏边| 黔西| 惠水县| 包头市| 迁安市| 巴东县| 于田县| 乐至县| 西和县|