今天在做一个正则表达式的解析时,突然看见一个?:的例子,就看不懂了,然后在网上查了下,竟然是非捕获型括号,意思就是匹配pattern,但不包含在match的group方法里面。
(?: pattern)是非捕获型括号 匹配pattern,但不捕获匹配结果。
(pattern )是捕获型括号。 匹配pattern,匹配pattern并捕获结果,自动获取组号
深入了解还得看例子:
例一:
@Test
public void test9(){
Pattern p = Pattern.compile("(d+,)(w+)");
String s = "123,abc-78,def";
Matcher m = p.matcher(s);
while(m.find())
{
System.out.println("m.group():"+m.group()); //打印所有
System.out.println("m.group(1):"+m.group(0)); //打印所有
System.out.println("m.group(1):"+m.group(1)); //打印数字的
System.out.println("m.group(2):"+m.group(2)); //打印字母的
}
}
输出:
m.group():123,abc
m.group(1):123,abc
m.group(1):123,
m.group(2):abc
m.group():78,def
m.group(1):78,def
m.group(1):78,
m.group(2):def
例二:
@Test
public void test10(){
Pattern p = Pattern.compile("(d+)-(d+)-(d+)s+([A-z]+)s+([A-z]+)s+(d+):(d+)");
String s = "2013-10-26 wei test 15:39";
Matcher m = p.matcher(s);
if(m.find())
{
System.out.println(m.groupCount());
System.out.println("m.group():"+m.group()); //2013-10-26 wei test 15:39
System.out.println("m.group():"+m.group(0)); //2013-10-26 wei test 15:39
System.out.println("m.group(1):"+m.group(1)); //2013
System.out.println("m.group(2):"+m.group(2)); //10
System.out.println("m.group(3):"+m.group(3)); //26
System.out.println("m.group(4):"+m.group(4)); //wei
System.out.println("m.group(5):"+m.group(5)); //test
System.out.println("m.group(6):"+m.group(6)); //15
System.out.println("m.group(7):"+m.group(7)); //39
}
}
例三:
@Test
public void test11(){
Pattern p = Pattern.compile("((d+)-(d+)-(d+))s+([A-z]+)s+([A-z]+)s+((d+):(d+))");
String s = "2013-10-26 wei test 15:39";
Matcher m = p.matcher(s);
if(m.find())
{
System.out.println("m.group():"+m.group()); //2013-10-26 wei test 15:39
System.out.println("m.group():"+m.group(0)); //2013-10-26 wei test 15:39
System.out.println("m.group(1):"+m.group(1)); //2013-10-26
System.out.println("m.group(2):"+m.group(2)); //2013
System.out.println("m.group(3):"+m.group(3)); //10
System.out.println("m.group(4):"+m.group(4)); //26
System.out.println("m.group(5):"+m.group(5)); //wei
System.out.println("m.group(6):"+m.group(6)); //test
System.out.println("m.group(7):"+m.group(7)); //15:39
System.out.println("m.group(8):"+m.group(8)); //15
System.out.println("m.group(9):"+m.group(9)); //39
}
}
例四:
@Test
public void test12(){
Pattern p = Pattern.compile("((?:d+)-(d+)-(d+))s+([A-z]+)s+([A-z]+)s+((d+):(d+))");
String s = "2013-10-26 wei test 15:39";
Matcher m = p.matcher(s);
if(m.find())
{
System.out.println(m.groupCount());//8
System.out.println("m.group():"+m.group()); //2013-10-26 wei test 15:39
System.out.println("m.group():"+m.group(0)); //2013-10-26 wei test 15:39
System.out.println("m.group(1):"+m.group(1)); //2013-10-26
System.out.println("m.group(2):"+m.group(2)); //10
System.out.println("m.group(3):"+m.group(3)); //26
System.out.println("m.group(4):"+m.group(4)); //wei
System.out.println("m.group(5):"+m.group(5)); //test
System.out.println("m.group(6):"+m.group(6)); //15:39
System.out.println("m.group(7):"+m.group(7)); //15
System.out.println("m.group(8):"+m.group(8)); //39
}
}
例五:
@Test
public void test13(){
Pattern p = Pattern.compile("(?:(?:d+)-(d+)-(d+))s+([A-z]+)s+([A-z]+)s+((d+):(d+))");
String s = "2013-10-26 wei test 15:39";
Matcher m = p.matcher(s);
if(m.find())
{
System.out.println(m.groupCount());//7
System.out.println("m.group():"+m.group()); //2013-10-26 wei test 15:39
System.out.println("m.group():"+m.group(0)); //2013-10-26 wei test 15:39
System.out.println("m.group(1):"+m.group(1)); //10
System.out.println("m.group(2):"+m.group(2)); //26
System.out.println("m.group(3):"+m.group(3)); //wei
System.out.println("m.group(4):"+m.group(4)); //test
System.out.println("m.group(5):"+m.group(5)); //15:39
System.out.println("m.group(6):"+m.group(6)); //15
System.out.println("m.group(7):"+m.group(7)); //39
}
}