我發現了類似的問題here。然而,我沒有得到它的工作:重疊正則表達式
我有一個像「my_token_string」字符串,需要一個正則表達式來返回令牌「my_」,「_token_」和「_string」。
請注意,我無法更改java代碼,因爲它是其他軟件的一部分。我能做的唯一的事情就是指定模式和組捕捉:-)
這是我測試過:
String p = "(?=(_[^_]*_?))";
int group = 1;
String test = "my_token_string";
Matcher m = Pattern.compile(p).matcher(test);
while (m.find()) {
System.out.println(m.group(group));
}
但當然這僅返回令牌「_token_」和「_string 」。
+1反正也沒有必要非捕獲組,因爲它們是內部的OP會使用,之前不是組。 – Pshemo 2013-03-21 14:44:59