CAN ANYBODY ELABORATE THE DIFFERENCE BETWEEN '\Z' AND '\Z' IN JAVA.UTIL.REGEX.PATTERN?

Lady Bird Johnson

Total Post:20

Points:140
Posted by  Lady Bird Johnson
Java 
 1064  View(s)
Ratings:
Rate this:

What is the difference between the JDK6 java.util.regex.Pattern's "Boundary matchers" '\z' and '\Z'?

Background: I was trying to read a UTF-8 encoded file's contents into a String.

  1. Kate Smith

    Post:24

    Points:168
    Re: Can anybody elaborate the difference between '\Z' and '\z' in java.util.regex.Pattern?

    Difference is anchor \Z matches the empty string just before the newline, while \z matches empty string after the newline.

    A very simple example to understand this is:

    System.out.println("abc\n".matches("abc\\Z\\n"));   // true
    System.out.println("abc\n".matches("abc\\z\\n"));   // false
    System.out.println("abc\n".matches("abc\\n\\z"));   // true

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!