SorenLantz
SorenLantz

Reputation: 177

How do I properly create a regex for String.matches() with escape characters?

I am trying to check if a string of length one is any of the following characters: "[", "\", "^", "_", single back-tick "`", or "]".

Right now I am trying to accomplish this with the following if statement:

if (character.matches("[[\\]^_`]")){
    isValid = false;
}

When I run my program I get the following error for the if statement:

java.util.regex.PatternSyntaxException: null (in java.util.regex.Pattern)

What is the correct syntax for a regex with escape characters?

Upvotes: 0

Views: 70

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

Your list has four characters that need special attention:

  • ^ is the inversion character. It must not be the first character in a character class, or it must be escaped.
  • \ is the escape character. It must be escaped for direct use.
  • [ starts a character class, so it must be escaped.
  • ] ends a character class, so it must be escaped.

Here is the "raw" regex:

[\[\]_`\\^]

Since you represent your regex as a Java string literal, all backslashes must be additionally escaped for the Java compiler:

if (character.matches("[\\[\\]_`\\\\^]")){
    isValid = false;
}

Upvotes: 2

RealSkeptic
RealSkeptic

Reputation: 34648

You need to escape the [, ] and \\ - the [ and ] so that the pattern compiler knows that they are not the special character class delimiters, and \\ because it's already being converted to one backslash because it's in a string literal, so to represent an escaped backslash in a pattern, you need to have no less than four consecutive backslashes.

So the resulting regex should be

"[\\[\\]\\\\^_`]"

(Test it on RegexPlanet - click on the "Java" button to test).

Upvotes: 0

Related Questions