日期:2014-05-17  浏览次数:20976 次

正则表达式匹配成功,如何在遍历所有捕获组时取得组名?
本帖最后由 u010936098 于 2013-06-28 09:31:20 编辑
有这样一段代码:

            Regex testXpress = new Regex(
                @"^\s*(?:" +
                    @"(?:" +
                         @"(?:(?<low_bound>\d+(?:\.\d+)?))" +
                         @"\s*(?:-|~)\s*" +
                         @"(?:(?<high_bound>\d+(?:\.\d+)?))" +
                     ")" +//0.78 - 1.23; 1-2.24; 4.3 -6; 5-9
                    @"|(?:(?:≤|(?:<=))\s*(?<high_bound>\d+(?:\.\d+)?))" +  //<= 1.23; ≤1.12; <=1; ≤ 2
                    @"|(?:(?:≥|(?:>=))\s*(?<low_bound>\d+(?:\.\d+)?))" +   //>= 1.23; ≥1.12; >=1; ≥ 2
                    @"|(?:(?:<|<)\s*(?<high_bound>\d+(?:\.\d+)?))" +  //< 1.23; <1.12; <1; < 2
                    @"|(?:(?:>|>)\s*(?<low_bound>\d+(?:\.\d+)?))" +   //> 1.23; >1.12; >1; > 2
                    @"|(?<accurate>\d+(?:\.\d+)?)" +                        // 12.45; 36
                @")\s*$"
                );

            Match mc = testXpress.Match(textBox1.Text);