Home > DeveloperSection > Forums > Can anybody elaborate the difference between '\Z' and '\z' in java.util.regex.Pattern?
Lady Bird Johnson
Lady Bird Johnson

Total Post:20

Posted on    October-14-2013 3:27 AM

 Java Java 

 1 Reply(s)
 1051  View(s)
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.

Kate Smith
Kate Smith

Total Post:24

Posted on    October-14-2013 3:59 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick