Java正则表达式:什么是非捕获组
来源:互联网 发布:企业网络搭建服务 编辑:程序博客网 时间:2024/04/28 13:30
//演示非捕获组概念
import java.util.regex.*;
public class NonCapture
...{
public static void main(String[] args)
...{
String inputStr = "abbabcd";
String patternStr = "(a(?:b*))+(c*)";
//String patternStr = "(a(b*))+(c*)";
//String patternStr = "(a)+(c*)";
// (?:b*) is a non-capturing group
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound)
...{
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++)
...{
String groupStr = matcher.group(i);
System.out.println("group"+i+" "+groupStr);
}
// group 0: abbabc
// group 1: ab
// group 2: c
}
System.out.println("groupCount()="+matcher.groupCount());
}
}
import java.util.regex.*;
public class NonCapture
...{
public static void main(String[] args)
...{
String inputStr = "abbabcd";
String patternStr = "(a(?:b*))+(c*)";
//String patternStr = "(a(b*))+(c*)";
//String patternStr = "(a)+(c*)";
// (?:b*) is a non-capturing group
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound)
...{
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++)
...{
String groupStr = matcher.group(i);
System.out.println("group"+i+" "+groupStr);
}
// group 0: abbabc
// group 1: ab
// group 2: c
}
System.out.println("groupCount()="+matcher.groupCount());
}
}
输出结果如下图。充足可以看到,非捕获组没有计算在 groupCount()在内
- Java正则表达式:什么是非捕获组
- Java正则表达式:什么是非捕获组解释
- java正则表达式捕获组
- java正则表达式 非捕获组详解
- java正则表达式 非捕获组详解
- java正则表达式 非捕获组详解
- Java 正则表达式 - 命名捕获组
- java正则表达式 非捕获组详解
- java正则表达式非捕获组详解
- java正则表达式非捕获组详解
- java正则表达式 非捕获组详解
- java正则表达式 非捕获组详解
- JAVA正则表达式-捕获组与非捕获组
- JAVA正则表达式-捕获组与非捕获组
- JAVA正则表达式-捕获组与非捕获组
- JAVA正则表达式-捕获组与非捕获组
- JAVA正则表达式-捕获组与非捕获组
- 正则表达式- 捕获组 非捕获组
- WinNT平台下直接读写磁盘扇区
- 解决ASP.NET缓存的两种方法
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!6
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!7
- C#执行SQL语句报超时已过期
- Java正则表达式:什么是非捕获组
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!8
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!9
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!10
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!10
- Jetspeed学习笔记
- ASP.NET 2.0中Gridview控件高级技巧
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!11
- ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!11