正则中的/num用法

来源:互联网 发布:默纳克解密软件 编辑:程序博客网 时间:2024/05/16 06:25


研究了半天,终于基本上明白了这个 /num的意思和用法,  查找与第num个"()"中的正则表达式相匹配的两个一样的值, 只有两个, 


        String s="is is the cost of of gasoline going up up";
//String regex ="(\\d+)(\\w+)\\1";
String regex ="\\b([a-z]+) \\1\\b";       //  \1代表第1 个()中的内容  , 这个 \\1 前面还有空格
 Pattern p=Pattern.compile(regex);
 Matcher m=p.matcher(s);
 while(m.find()){
  System.out.println(m.group());