BJ Dela Cruz
BJ Dela Cruz

Reputation: 5354

Java Regex Explanation

Can someone please explain the meaning of \\A and \\z to me? I am assuming that they have a special meaning in this regular expression because they are being escaped (but I could be wrong and they could just stand for A and z, respectively). Thanks!

private static final Pattern PATTERN = Pattern.compile("\\A(\\d+)\\.(\\d+)\\z");

Upvotes: 5

Views: 5929

Answers (3)

check out this reference

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

according to that \A matches the start of input, and \z matches the end of input.

the reason why they have \\ before them instead of just 1 \ is because the \ is also being escaped.

Upvotes: 1

ruakh
ruakh

Reputation: 183602

This Java expression:

Pattern.compile("\\A(\\d+)\\.(\\d+)\\z")

produces this regex:

\A(\d+)\.(\d+)\z

where \A means "start-of-string" and \z means "end-of-string".

So, that pattern matches any string consisting of one or more digits, plus a decimal point, plus one or more digits.

For details about all aspects of Java regex notation, see the Javadoc for java.util.regex.Pattern.

Upvotes: 4

Tim Pietzcker
Tim Pietzcker

Reputation: 336478

\A means "start of string", and \z means "end of string".

You might have seen ^ and $ in this context, but their meaning can vary: If you compile a regex using Pattern.MULTILINE, then they change their meaning to "start of line" and "end of line". The meaning of \A and \z never changes.

There also exists \Z which means "end of string, before any trailing newlines", which is similar to what $ does in multiline mode (where it matches right before the line-ending newline character, if it's there).

Upvotes: 12

Related Questions