4.1. 捕获组(Capturing Group)
在Pattern当中的正则表达当中,通过使用括号,我们可以在原来的表达式当中定义子表达式,或者称为Capturing Group。通过Matcher,我们还可以直接提取某一个Capturing Group的内容。
public class Test {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\S \\s (\\S )\\s \\S ");
String inputString = "hey there feller";
Matcher matcher = p.matcher(inputString);
while (matcher.find()) {
int start = matcher.start(1);
int end = matcher.end(1);
String matched = inputString.substring(start, end);
System.out.println(matched);
}
System.out.println("===== Using Group: =====");
matcher.reset();
while (matcher.find()) {
String matched = matcher.group(1);
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



