(7)组与非捕获组以下提供一些简单的示例: string x = "Live for nothing,die for something"; string y = "Live for nothing,die for somebody"; Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$"); Console.WriteLine("x match count:" + r.Matches(x).Count);//1 Console.WriteLine("y match count:" + r.Matches(y).Count);//0 //正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:thing } //获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。 string x = "Live for nothing,die for something"; Regex r = new Regex(@"^Live for no(?[a-z]{5}),die for some\1$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//输出:thing } //可根据组名进行索引。使用以下格式为标识一个组的名称(?…)。 string x = "Live for nothing nothing"; Regex r = new Regex(@"([a-z]+) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "$1"); Console.WriteLine("var x:" + x);//输出:Live for nothing } //删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用: string x = "Live for nothing nothing"; Regex r = new Regex(@"(?[a-z]+) \1"); if (r.IsMatch(x)) { x = r.Replace(x, "${g1}"); Console.WriteLine("var x:" + x);//输出:Live for nothing } string x = "Live for nothing"; Regex r = new Regex(@"^Live for no(?:[a-z]{5})$"); if (r.IsMatch(x)) { Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:(空) } //在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。
onpaste="return !/[^\w\u4e00-\u9fa5]/g.test(window.clipboardData.getData('Text'))"
ondragenter="return false"/>
string y = "Live for nothing,die for somebody";
Regex r = new Regex(@"^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
Console.WriteLine("x match count:" + r.Matches(x).Count);//1
Console.WriteLine("y match count:" + r.Matches(y).Count);//0
//正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no([a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{
Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:thing
}
//获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。
string x = "Live for nothing,die for something";
Regex r = new Regex(@"^Live for no(?[a-z]{5}),die for some\1$");
if (r.IsMatch(x))
{
Console.WriteLine("group1 value:" + r.Match(x).Groups["g1"].Value);//输出:thing
}
//可根据组名进行索引。使用以下格式为标识一个组的名称(?…)。
string x = "Live for nothing nothing";
Regex r = new Regex(@"([a-z]+) \1");
if (r.IsMatch(x))
{
x = r.Replace(x, "$1");
Console.WriteLine("var x:" + x);//输出:Live for nothing
}
//删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用:
string x = "Live for nothing nothing";
Regex r = new Regex(@"(?[a-z]+) \1");
if (r.IsMatch(x))
{
x = r.Replace(x, "${g1}");
Console.WriteLine("var x:" + x);//输出:Live for nothing
}
string x = "Live for nothing";
Regex r = new Regex(@"^Live for no(?:[a-z]{5})$");
if (r.IsMatch(x))
{
Console.WriteLine("group1 value:" + r.Match(x).Groups[1].Value);//输出:(空)
}
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。
我还是没理解 ?:存在的意义!能否解释
非捕获组的作用有两个,第一个比较常用,第二个了解一下即可(1)、节省系统资源,提高效率
在使用“ ¦”表示“或”的关系时,稍微复杂的情况,需要用()来限制“ ¦”的作用范围,否则即表示“ ¦”的左右两侧整体为“或”的关系,这是题外话,这里不详细说明了,还有用{num}来表达式匹配次数时,有时前面也要用到()限制作用范围
而使用()来限制作用范围的同时,默认情况下会把匹配到的结果保存到一个捕获组里,而大多数时候,我们是不需要保存这部分内容的,这就带来一定的副作用,浪费了系统资源,降低了效率
非捕获组的一个作用就是用来消除这种副作用的,(?:exp)用来匹配exp所表示的规则,但不将匹配结果保存到捕获组里比如匹配HH:mm:ss这样的时间
MessageBox.Show(Regex.IsMatch("18:23:55", "^(?:[01][0-9]|2[0-3])(?::[0-5][0-9]){2}$").ToString());
(?:[01][0-9] ¦2[0-3])验证小时部分是否符合规则,但不会将匹配结果保存到捕获组里
(?::[0-5][0-9]){2}验证了分秒部分,但不会将匹配结果保存到捕获组里(2)、在使用Regex.Split方法时,起到与RegexOptions .ExplicitCapture参数相同的作用,这个用得不多~~
//给个简单的例子
string str = @"lasddfiei12542asdasdfs85452ea"; Regex objRegex1 = new Regex(@"(\d+)");
Regex objRegex2 = new Regex(@"(?:\d+)"); Response.Write(String.Format("Regex 1 : {0}<br />", objRegex1.ToString())); MatchCollection objMatches = objRegex1.Matches(str);
Int32 index = 0;
foreach (Match objMatch in objMatches)
{
Response.Write(String.Format("Result {1} : {0}<br />", objMatch.Result("$1"),++index));
} Response.Write("<hr />");
index = 0; Response.Write(String.Format("Regex 2 : {0}<br />", objRegex2.ToString())); objMatches = objRegex2.Matches(str); foreach (Match objMatch in objMatches)
{
Response.Write(String.Format("Result {1} : {0}<br />", objMatch.Result("$1"), ++index));
}
//结果
/*
这个捕获了结果
Regex 1 : (\d+)
Result 1 : 12542
Result 2 : 85452--------------------------------------------------------------------------------
//这个没有捕获了结果 输出原来的字符串。
Regex 2 : (?:\d+)
Result 1 : $1
Result 2 : $1*/
//捕获组:正则捕获之后存到内存中
string input = "捕获组例:string input=";
Regex re = new Regex(@"([a-z]+)\s+[a-z]+");
Match m = re.Match(input);
GroupCollection gc = m.Groups;
foreach (Group g in gc)
{
Console.WriteLine(g.Value);
}
/*结果
string input GroupCollection中的第一个组是匹配的字符串本身 不管正则表达式当中有没有捕获组 这里的第一个组永远是它本身
string 这里才是正则表达式中那个捕获组中捕获的值
*/
//非捕获组:正则中组匹配的内容不捕获 不放到内存中
string input = "捕获组例:string input=";
Regex re = new Regex(@"(?:[a-z]+)\s+[a-z]+");
Match m = re.Match(input);
GroupCollection gc = m.Groups;
foreach (Group g in gc)
{
Console.WriteLine(g.Value);
}
/*结果
string input 只有这一个 因为组是非捕获组
*/
C# code
//捕获组:正则捕获之后存到内存中
string input = "捕获组例:string input=";
Regex re = new Regex(@"([a-z]+)\s+[a-z]+");
Match m = re.Match(input);
GroupCollection gc = m.Groups;
foreach (Group g in gc)
{
Console.WriteLine(g.Value);
}
/*结果
string input GroupCollection中的第一…
[/Quote
是不是 说白了就是 只要是 (?:xxx) 这种形式 GroupCollection 的时候 就不取xxx了?
不能反向引用。