Java正则表达式捕获重复的组

时间:2019-08-21 12:36:35

标签: java

我无法通过正则表达式捕获第2组中的所有数据,包括所有字符,数字,空格和特殊字符

尝试正则表达式

 final String regex = "^:(.*?)//(.*[\\s\\S]?)";
    String line1 = ":Humpty Dumpty sat on a wall";
    String line2 = "//Humpty Dumpty had a great fall";
    String rhyme = line1 +  line2+"\n"+ "ssdsds"+"\n";
    final String value = rhyme.replaceAll(regex , "$2");
 final boolean formatIdentified =   rhyme.matches(formatRegex);
System.out.println(formatIdentified);//returns false

我期望的值

"Humpty Dumpty had a great fall
 ssdsds
"

更正后的正则表达式应使用格式:abc//xxxx,输出应为xxxx

2 个答案:

答案 0 :(得分:0)

String.replaceAllString.matches完全有可能以不同的方式处理多行字符串的终止符,即换行符与字符串结尾,这就是value可能会打印预期的原因结果,但matches打印错误。

我会更加明确,直接使用PatternMatcher而不是通过String进行代理:

        String rhyme =
                ":Humpty Dumpty sat on a wall\n" +
                "//Humpty Dumpty had a great fall\n" +
                "ssdsds\n";

        Pattern pattern = Pattern.compile("^:(.*?)//(.*[\\s\\S]?)", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(rhyme);
        if (matcher.find()) {
            System.out.println(matcher.group(2));
        } else {
            System.out.println("[Pattern not found]");
        }

这将输出:

   Humpty Dumpty had a great fall
   ssdsds

如果您希望只匹配一个新行结尾,则只需将标志更改为Pattern.MULTILINE。

   Pattern pattern = Pattern.compile("^:(.*?)//(.*[\\s\\S]?)", Pattern.MULTILINE);

哪个会输出:

   Humpty Dumpty had a great fall

答案 1 :(得分:0)

这将提供您想要的输出。

      // ignore everything up to // and then include // and all following
      // in capture group 1.
      final String regex = ".*(//.*)";
      String line1 = ":Humpty Dumpty sat on a wall";
      String line2 = "//Humpty Dumpty had a great fall";
      String rhyme = line1 + line2 + "\n" + "ssdsds" + "\n";
      final String value = rhyme.replaceAll(regex, "$1");
      System.out.println(value);
          // or the follwing if you want the double quotes.
      System.out.println("\"" + value + "\"");