---
title: "JAVA中的REGEXP非捕获组"
author: "orange5458"
date: "2011-11-02T23:30:48Z"
tags: []
categories: ["编程语言"]
slug: "java-regexp"
draft: false
lastmod: "2011-11-02T23:33:52Z"
id: 1231965
url: "https://orange5458.iteye.com/blog/1231965"
---

1.简介

 JAVA中REGEXP的非捕获组为(?)开头的组，包含以下8种

1）(?:X) X，作为非捕获组  
与捕获组 ( ) 的意思一样也是将其作为一组进行处理，与捕获组的区别在于不捕获匹配的文本，  
仅仅作为分组。  
比如：要匹配 123123 这个，就可以写为 (123)\\1 使用反向引用，这时只能用捕获组，在匹配  
123 后会保留在内存中，便于反向引用，而 (?:123) 在匹配完后则不会保留，区别仅在于此。

2）(?idmsux-idmsux) Nothing，但是将匹配标志i d m s u x on - off  
用于标志匹配，比如：表达式 (?i)abc(?-i)def 这时，(?i) 打开不区分大小写开关，abc 匹配  
不区分大小地进行匹配，(?-i) 关闭标志，恢复不区分大小写，这时的 def 只能匹配 def

3）(?idmsux-idmsux:X) X，作为带有给定标志 i d m s u x on - off  
与上面的类似，上面的表达式，可以改写成为：(?i:abc)def，或者 (?i)abc(?-i:def)

4）(?=X) X，通过零宽度的正 lookahead  
5）(?!X) X，通过零宽度的负 lookahead  
(?=X) 表示当前位置（即字符的缝隙）后面允许出现的字符，比如：表示式 a(?=b)，在字符串为  
ab 时，可能匹配 a，后面的 (?=b) 表示，a 后面的缝隙，可以看作是零宽度。  
(?!X) 表示当前位置后面不允许出现的字符

6）(? &lt;=X) X，通过零宽度的正 lookbehind  
7）(? &lt;!X) X，通过零宽度的负 lookbehind  
这两个与上面两个类似，上面两个是向后看，这个是向前看，即前面可以出现的字符

8）(?&gt;X) X，作为独立的非捕获组  
匹配成功不进行回溯，这个比较复杂，也侵占量词“+”可以通用，比如：\\d++ 可以写为 (?&gt;\\d+)。

[http://www.regular-expressions.info/atomic.html](http://www.regular-expressions.info/atomic.html)

其中说的示例：来看 /\\b(integer|insert|in)\\b/ 匹配 integers 过程，第一个，当integer\\b匹配到s时失败，然后字符串(integers)会回溯到i，再接着第二个(insert)去匹配。而把模式写成 /\\b(?&gt;integer|insert|in)\\b/ 在刚才的第一个匹配失败，字符串(integers)不会回溯了，也不会有第二个去匹配了，所有速度会快一点点。

但是写 (?&gt;X) 这种式子时要注意，是从左到右看的。/\\b(?&gt;integer|insert|in)\\b/ ，与 /\\b(?&gt;in|integer|insert)\\b/ 去匹配 insert，结果会不一样，前者可以匹配到，后者不能，什么原因自己分析下。一但匹配失败就会跳过，所以应该长的写在表达式前面。

 

2.源代码

 

```
package com.siyuan.jdktest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("------------------------------");
		Pattern pattern1 = Pattern.compile("(?:123)"); //1）
		Matcher matcher1 = pattern1.matcher("123");
		System.out.println(matcher1.matches());
		System.out.println(matcher1.group(0));
//		System.out.println(matcher1.group(1)); //ERROR
		
		System.out.println("------------------------------");
//		Pattern pattern2 = Pattern.compile("(?i)abc(?-i)def"); //2）
		Pattern pattern2 = Pattern.compile("(?i:abc)(?-i:def)"); //3）
		Matcher matcher2 = pattern2.matcher("abcdef"); //true
//		Matcher matcher2 = pattern2.matcher("ABCdef"); //true
//		Matcher matcher2 = pattern2.matcher("abcDEF"); //false
		System.out.println(matcher2.matches());
		System.out.println(matcher2.group(0));
//		System.out.println(matcher2.group(1));//ERROR
//		System.out.println(matcher2.group(2));//ERROR
		
		System.out.println("------------------------------");
//		Pattern pattern3 = Pattern.compile("a(?=b)"); //4）
//		Pattern pattern3 = Pattern.compile("a(?!b)"); //5）
//		Pattern pattern3 = Pattern.compile("(?<=b)a"); //6）
		Pattern pattern3 = Pattern.compile("(?<!b)a"); //7）
		String test3 = "aacababaA";
		Matcher matcher3 = pattern3.matcher(test3);
		while (matcher3.find()) {
			System.out.println("-------------------");
			System.out.println(matcher3.group());
			System.out.println(matcher3.start());
		}
	  
		System.out.println("------------------------------");
		Pattern pattern4 = Pattern.compile("(?>integer|insert|in)"); //8）
//		Pattern pattern4 = Pattern.compile("\\b(?>integer|insert|in)\\b"); //8）
//		Pattern pattern4 = Pattern.compile("\\b(?>in|integer|insert)\\b"); //8）
//		Pattern pattern4 = Pattern.compile("\\b(?>insert|integer|in)\\b"); //8）
		String test4 = "insert";
		Matcher matcher4 = pattern4.matcher(test4);
		while (matcher4.find()) {
			System.out.println("-------------------");
			System.out.println(matcher4.group());
			System.out.println(matcher4.start());
		}

		
	}

}
```

 

 

3.运行结果

 

```
------------------------------
true
123
------------------------------
true
abcdef
------------------------------
-------------------
a
0
-------------------
a
1
-------------------
a
3
------------------------------
-------------------
insert
0
```

 

 

4.参考资料

 

[http://hi.baidu.com/victorlin23/blog/item/289cfc4333310c199213c615.html](http://hi.baidu.com/victorlin23/blog/item/289cfc4333310c199213c615.html)