5

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

3 Answers 3

12

\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).

Sign up to request clarification or add additional context in comments.

1 Comment

I was about to ask whether \A and \z are similar to ^ and $. Thanks for your explanation!
4

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.