正則表達(dá)式中的組是很重要的一個概念,它是我們通向高級正則應(yīng)用的的橋梁。
組的概念
一個正則表達(dá)式匹配結(jié)果可以分成多個部分,這就是組(Group)的目的。能夠靈活的使用組后,你會發(fā)現(xiàn)Regex真是很方便,也很強大。
先舉個例子
- public static void Main()
- {
- string s = "2005-2-21";
- Regex reg = new Regex(@"(?<y>\d{4})-(?<m>\d{1,2})-(?<d>\d{1,2})",RegexOptions.Compiled);
- Match match = reg.Match(s);
- int year = int.Parse(match.Groups["y"].Value);
- int month = int.Parse(match.Groups["m"].Value);
- int day = int .Parse(match.Groups["d"].Value);
- DateTime time = new DateTime(year,month,day);
- Console.WriteLine(time);
- Console.ReadLine();
- }
以上的例子通過組來實現(xiàn)分析一個字符串,并把其轉(zhuǎn)化為一個DateTime實例,當(dāng)然,這個功能用DateTime.Parse方法就能很方便的實現(xiàn)。
在這個例子中,我把一次Match結(jié)果用(?<name>)的方式分成三個組"y","m","d"分別代表年、月、日。
現(xiàn)在我們已經(jīng)有了組的概念了,再來看如何分組,很簡單的,除了上在的辦法,我們可以用一對括號就定義出一個組,比如上例可以改成:
- public static void Main()
- {
- string s = "2005-2-21";
- Regex reg = new Regex(@"(\d{4})-(\d{1,2})-(\d{1,2})",RegexOptions.Compiled);
- Match match = reg.Match(s);
- int year = int.Parse(match.Groups[1].Value);
- int month = int.Parse(match.Groups[2].Value);
- int day = int .Parse(match.Groups[3].Value);
- DateTime time = new DateTime(year,month,day);
- Console.WriteLine(time);
- Console.ReadLine();
- }
從上例可以看出,第一個括號對包涵的組被自動編號為1,后面的括號依次編號為2、3……
- public static void Main()
- {
- string s = "2005-2-21";
- Regex reg = new Regex(@"(?<2>\d{4})-(?<1>\d{1,2})-(?<3>\d{1,2})",RegexOptions.Compiled);
- Match match = reg.Match(s);
- int year = int.Parse(match.Groups[2].Value);
- int month = int.Parse(match.Groups[1].Value);
- int day = int .Parse(match.Groups[3].Value);
- DateTime time = new DateTime(year,month,day);
- Console.WriteLine(time);
- Console.ReadLine();
- }
再看上例,我們用(?<數(shù)字>)的方式手工給每個括號對的組編號,(注意我定義1和2的位置時不是從左到右定義的)
通過以上三例,我們知道了給Regex定義Group的三種辦法以及相應(yīng)的引用組匹配結(jié)果的方式。
然后,關(guān)于組定義,還有兩點請注意:
1、因為括號用于定義組了,所以如果要匹配"("和")",請使用"\("和"\)"(關(guān)于所有特殊字符的定義,請查看相關(guān)Regex expression幫助文檔)。
2、如果定義Regex時,使用了ExplicitCapture選項,則第二個例子不會成功,因為此選項要求顯式定義了編號或名字的組才捕獲并保存結(jié)果,如果你沒有定義ExplicitCapture選項,而有時又定義了類式于(A|B)這樣的部分在表達(dá)式,而這個(A|B)你又并不想捕獲結(jié)果,那么可以使用"不捕獲的組"語法,即定義成(?:)的方式,針對于(A|B),你可以這樣來定義以達(dá)到不捕獲并保存它到Group集合中的目的--(?:A|B)。