import java.io.*;
import java.util.regex.*;
public class test
{
    public static void main(String[] args)
    {
String str = "go go";
        String regex="\\b(?<Word>\\w+)\\b\\s+\\k<Word>\\b"; 
        Matcher m=Pattern.compile(regex).matcher(str);
       while(m.find()){
            System.out.println(m.group());
            
        }
    }
}

解决方案 »

  1.   

    ? <Word>这是做什么?向前引用要用 $1,$2...
      

  2.   

    import java.util.regex.*;public class Test{

        public static void main(String args[])  {
         String str=" go go";
         String regex="\\b(\\w+)\\b\\s*\\1\\b";
         Matcher m=Pattern.compile(regex).matcher(str); 
         while(m.find()){ 
                System.out.println(m.group()); 
                
            }     }
    }
      

  3.   

    (?<name>Expression) 命名捕获组
    \k<name>  命名捕获组反向引用
    命名捕获组目前只有.NET和PHP等有限的几种语言支持,Java是不支持的
    据说是在Java7.0中会提供对命名捕获组的支持,目前Java中只能使用普通捕获组