Java RegEx – How to Escape and Match bracket example shows how to escape and match square bracket and round bracket (parentheses) literally in Java regular expression pattern.
How to match a square bracket using regex in Java?
The square brackets in Java’s regular expression have a special meaning. They are used to define a character class. See the below-given regex pattern for example.
1 |
[^0-9] |
This pattern matches with any character that is not a digit between 0 to 9.
1 2 3 4 5 6 7 8 9 10 |
package com.javacodeexamples.regex; public class RegExEscapeBracket { public static void main(String[] args) { String str = "123abc456"; System.out.println( str.replaceAll("[^0-9]", "") ); } } |
Output
1 |
123456 |
As you can see from the output, all the non-digit characters were removed from the string.
Since the square brackets are used to define a character class in a regex, we cannot directly mention that when we want to match it literally. See the below example.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javacodeexamples.regex; public class RegExEscapeBracket { public static void main(String[] args) { String str = "[One],[two]"; str = str.replaceAll("[", ""); System.out.println(str); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.clazz(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.lang.String.replaceAll(Unknown Source) |
As you can see from the output, the code throws java.util.regex.PatternSyntaxException: Unclosed character class near index exception, since it was expecting a character class when it encountered the opening square bracket.
In order to match the square bracket literally, we need to escape it like “\\[” as given below.
1 2 3 4 |
String str = "[One],[two]"; str = str.replaceAll("\\[", ""); System.out.println(str); |
Output
1 |
One],two] |
How to match a round bracket or parentheses using regex in Java?
Just like the square bracket, a round bracket has also a special meaning in the regex. It is used to define a regex group which we can later extract.
1 2 3 4 |
String str = "(One), (Two)"; str = str.replaceAll("(", ""); System.out.println(str); |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1 ( at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.accept(Unknown Source) at java.util.regex.Pattern.group0(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.lang.String.replaceAll(Unknown Source) |
An exception is thrown complaining about an unclosed group since it was expecting a group to be defined using the parentheses. This time the exception is java.util.regex.PatternSyntaxException: Unclosed group near index 1. Just like the square bracket, we also need to escape the round bracket like “\\(” in order to match it literally.
1 2 3 4 |
String str = "(One), (Two)"; str = str.replaceAll("\\(", ""); System.out.println(str); |
Output
1 |
One), Two) |
We need to escape all metacharacters in such a way wherever we are using the regex, for example, in the string split method, string replaceAll method, and while compiling a pattern.
If you want to learn more about the regex, please visit the Java RegEx tutorial.
Please let me know your views in the comments section below.