regexjava-8

How to use \R in java 8 regex


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);

Solution

  • 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 .*