简体   繁体   中英

Java regular expression to capture repetive groups

I am not able to capture all the data including all chars ,digits, spaces and special characters from group 2 through regular expression

tried regular expressions

 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

value I am expecting

"Humpty Dumpty had a great fall
 ssdsds
"

corrected regular expression should work with format :abc//xxxx , output should be xxxx .

It is entirely possible that String.replaceAll and String.matches are handling the terminator for multi-line strings differently, ie newline vs end of the string, which is why value may print the expected result but matches prints false.

I would be more explicit and use Pattern and Matcher directly rather than proxied through 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]");
        }

This will output:

   Humpty Dumpty had a great fall
   ssdsds

If you are expecting to only match up to a new line ending you just need to change the flag to Pattern.MULTILINE.

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

Which will output:

   Humpty Dumpty had a great fall

This provides the output you wanted.

      // 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 + "\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM