新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#正則表達式之擇一匹配淺析
C#正則表達式之擇一匹配都是什么呢?具體的操作有哪些呢?

創(chuàng)新互聯(lián)專注于涪陵網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供涪陵營銷型網(wǎng)站建設,涪陵網(wǎng)站制作、涪陵網(wǎng)頁設計、涪陵網(wǎng)站官網(wǎng)定制、微信小程序服務,打造涪陵網(wǎng)絡公司原創(chuàng)品牌,更為您提供涪陵網(wǎng)站排名全網(wǎng)營銷落地服務。
C#正則表達式中的 (|) 符號似乎沒有一個專門的稱謂,姑且稱之為“擇一匹配”吧。事實上,像[a-z]也是一種擇一匹配,只不過它只能匹配單個字符,而(|)則提供了更大的范圍,(ab|xy)表示匹配ab或匹配xy。注意“|”與“()”在此是一個整體。
下面提供一些簡單的C#正則表達式之擇一匹配示例:
- string x = "0";
- string y = "0.23";
- string z = "100";
- string a = "100.01";
- string b = "9.9";
- string c = "99.9";
- string d = "99.";
- string e = "00.1";
- Regex r = new Regex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$");
- Console.WriteLine("x match count:" + r.Matches(x).Count);//1
- Console.WriteLine("y match count:" + r.Matches(y).Count);//1
- Console.WriteLine("z match count:" + r.Matches(z).Count);//1
- Console.WriteLine("a match count:" + r.Matches(a).Count);//0
- Console.WriteLine("b match count:" + r.Matches(b).Count);//1
- Console.WriteLine("c match count:" + r.Matches(c).Count);//1
- Console.WriteLine("d match count:" + r.Matches(d).Count);//0
- Console.WriteLine("e match count:" + r.Matches(e).Count);//0
- //匹配0到100的數(shù)。最外層的括號內包含兩部分“(100(.0+)*)”,
- //“([1-9]?[0-9])(\.\d+)*”,
- //這兩部分是“OR”的關系,
- //即正則表達式引擎會先嘗試匹配100,
- //如果失敗,則嘗試匹配后一個表達式(表示[0,100)范圍中的數(shù)字)。
C#正則表達式之擇一匹配使用的基本內容就向你介紹到這里,希望對你了解和學習C#正則表達式有所幫助。
文章標題:C#正則表達式之擇一匹配淺析
標題路徑:http://www.5511xx.com/article/dpicshp.html


咨詢
建站咨詢
