Whats the difference between \z and \Z in a regular expression and when and how do I use it?

Even though \Z and $ only match at the end of the string (when
the option for the caret and dollar to match at embedded line breaks is
off), there is one exception. If the string ends with a line break,
then \Z and $ will match at the position before that line break,
rather than at the very end of the string.

This “enhancement” was introduced by Perl, and is copied by many regex
flavors, including Java, .NET and PCRE. In Perl, when reading a line
from a file, the resulting string will end with a line break. Reading
a line from a file with the text “joe” results in the string joe\n.
When applied to this string, both ^[a-z]+$ and \A[a-z]+\Z will
match “joe”.

If you only want a match at the absolute very end of the string, use
\z (lower case z instead of upper case Z). \A[a-z]+\z does not
match joe\n. \z matches after the line break, which is not matched
by the character class.

http://www.regular-expressions.info/anchors.html

The way I read this "StackOverflow\n".matches("StackOverflow\\z") should return false because your pattern does not include the newline.

"StackOverflow\n".matches("StackOverflow\\z\\n") => false
"StackOverflow\n".matches("StackOverflow\\Z\\n") => true

Leave a Comment