1

I am trying to use the new \R regex matcher from java 8. However, for the following code :

public static void main(String[] args)
  {
    String s = "This is \r\n a String with \n Different Newlines \r and other things.";
    System.out.println(s);
    System.out.println(Pattern.matches("\\R", s));
    if (Pattern.matches("\\R", s)) // <-- is always false
    {
      System.out.println("Matched");
    }

    System.out.println(s.replaceAll("\\R", "<br/>")); //This is a String with <br/> Different Newlines <br/> and other things.
  }

The Pattern.matches always returns false, where as the replaceAll method does seem to find a match and does what I want it to. How do I make the Pattern.matches work ?

I have also tried the long about way and still can't get it to work :

   Pattern p = Pattern.compile("\\R");
    Matcher m = p.matcher(s);
    boolean b = m.matches();
    System.out.println(b);
happybuddha
  • 1,271
  • 2
  • 20
  • 40
  • I was looking for information about the Java regex construct `\\R` and found this question, while the duplicate does not mention it. This accepted answer explains what `\\R` is, but also talks about the difference between `.matches` and `.find` as in the duplicate. – Stephen P Apr 06 '23 at 22:38

1 Answers1

6

Well matches (both in String and Matchers classes) attempts to match the complete input string.

You need to use matcher.find instead:

Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.find();
System.out.println(b);

From Java docs:

\R Matches any Unicode line-break sequence, that is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

PS; If you want to know if input contains a line-break then this one liner will work for you:

boolean b = s.matches("(?s).*?\\R.*");

Note use of .* on either side of \R to make sure we are matching complete input. Also you need (?s) to enable DOTALL mode to be able to match multiline string with .*

anubhava
  • 761,203
  • 64
  • 569
  • 643