• Java characters that have to be escaped in regular expressions are:
    \.[]{}()<>*+-=!?^$|
  • Two of the closing brackets (] and }) only have to be escaped after opening the same type of bracket.
  • In []-brackets some characters (like + and -) do sometimes work without escape.
Answer from Tobi G. on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › guide to escaping characters in java regexps
Guide to Escaping Characters in Java RegExps | Baeldung
July 22, 2024 - In other words, it escapes all the metacharacters present in the regex pattern for us. It is doing a similar job to \Q & \E. The pipe character is escaped by the Pattern.quote() method and the split() interprets it as a String literal by which it divides the input.
Discussions

Regex for special characters in java - Stack Overflow
The second regex is a problem because you are trying to use reserved characters without escaping them. You can enclose them in [] where most characters (not all) do not have special meanings, but the whole thing would look very messy and you have to check that you haven't missed out any punctuation. More on stackoverflow.com
🌐 stackoverflow.com
java - Regex pattern including all special characters - Stack Overflow
I want to write a simple regular expression to check if in given string exist any special character. My regex works but I don't know why it also includes all numbers, so when I put some number it r... More on stackoverflow.com
🌐 stackoverflow.com
android - Check for special characters in a string using regex in java - Stack Overflow
How to check for special chars in a string? I am checking for just empty spaces using regex but when i enter special chars it's considering them as space. Below is my code private boolean More on stackoverflow.com
🌐 stackoverflow.com
regex - find out for special characters in Java - Stack Overflow
where keyName is a String from which I am searching for presence special characters · Can anyone provide Regex for Java Pattern-Matcher? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Delft Stack
delftstack.com › home › howto › java › java regex special charcters
How to Handle Regex Special Characters in Java | Delft Stack
February 14, 2024 - Regex (Regular Expression) is a useful tool for manipulating, searching, and processing text strings. It simplifies and reduces the number of lines in a program. In this article, we will explore various methods of utilizing regex special characters in Java, including escaping with backslash, character classes, negation in character classes, the dot metacharacter, asterisk quantifier, plus quantifier, question mark quantifier, anchors, pipe alternation, and parentheses for grouping.
🌐
Regular-Expressions.info
regular-expressions.info › characters.html
Regex Tutorial: Literal Characters and Special Characters
If you forget to escape a special character where its use is not allowed, such as in +1, then you will get an error message. There are a few exceptions, though. BRE-style flavors, for example, *1 as a literal regex. Most regular expression flavors treat the brace { as a literal character, unless it is part of a repetition operator like a{1,3}. So you generally do not need to escape it with a backslash, though you can do so if you want. But there are a few exceptions. Java ...
Find elsewhere
🌐
Medium
medium.com › sina-ahmadi › java-regex-6e4d073aab85
Java RegEx. special characters issue in Java split… | by Sina | My journey as a software developer | Medium
June 20, 2018 - To escape a character in Java, you should use two backslashes “\\”. I have done the below steps to escape the asterisk character and fix this issue in my code: Replace all special characters using Java’s “replaceAll” method in the ...
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
to escape special regex characters, e.g., \. for ., \+ for +, \* for *, \? for ?. You also need to write \\ for \ in regex to avoid ambiguity. Regex also recognizes \n for newline, \t for tab, etc. Take note that in many programming languages (C, Java, Python), backslash (\) is also used for escape sequences in string, e.g., "\n" for newline, "\t" for tab, and you also need to write "\\" for \. Consequently, to write regex pattern \\ (which matches one \) in these languages, you need to write "\\\\" (two levels of escape!!!).
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-a-string-contains-special-characters-in-java-559981
How to Check If a String Contains Special Characters in Java | LabEx
This is a practical application of regex for data analysis or validation. We will use the same regex pattern from Step 1 ([^a-zA-Z0-9\\s]) to identify special characters and iterate through the string using the Matcher to count how many times the pattern is found. Open the HelloJava.java file in the WebIDE editor.
🌐
Blogger
javahungry.blogspot.com › 2020 › 06 › check-string-contains-special-characters.html
How to Check String Contains Special Characters in Java | Java Hungry
According to YourDictionary, non-alphabetic and non-numeric characters such as #, @ are called special characters. import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaHungry { public static void main(String args[]) { String inputString = "Alive*is*Awesome$"; Pattern ...
🌐
javaspring
javaspring.net › blog › java-regular-expression-to-search-a-list-of-special-characters
Mastering Java Regular Expressions for Searching Special Characters — javaspring.net
In Java regular expressions, some characters have special meanings. For example, . matches any single character, * matches zero or more occurrences of the preceding element, and + matches one or more occurrences.
🌐
javathinking
javathinking.com › blog › escaping-special-characters-in-java-regular-expressions
How to Escape Special Characters in Java Regular Expressions: Methods & Libraries to Avoid Manual Escaping — javathinking.com
To match them literally, you must escape them with a backslash (\). Here’s a breakdown: Manual escaping requires adding a backslash before each special character. However, Java strings use \ as their own escape character (e.g., \n for newline).
🌐
Coderanch
coderanch.com › t › 406470 › java › Regex-replacing-special-characters
Regex for replacing special characters (Beginning Java forum at Coderanch)
March 19, 2007 - Originally posted by srikanth ramu: This will replace all the special characters except dot (.) String newName = name.replaceAll("[\\W]&&[^.]","_"); Thanks for replying. But I want all the characters apart from A-Z a-z 0-9 . (dot) _ (underscore) - (hyphen) to be replaced with an _ (underscore) ... To invert a character class, add '^' to the beginning of it: Also, you want to use the + (one or more) quantifier instead of * (zero or more).
🌐
3SL
threesl.com › home › blog › special characters in regexes and how to escape them
Special characters in regexes and how to escape them
February 3, 2023 - If you want to match 1+2=3, you need to use a backslash (\) to escape the + as this character has a special meaning (Match one or more of the previous). To match the 1+2=3 as one string you would need to use the regex 1\+2=3 · For further information on using regexes in Cradle see our online help.
Top answer
1 of 8
43

I wrote this pattern:

Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");

And use it in this method:

String escapeSpecialRegexChars(String str) {

    return SPECIAL_REGEX_CHARS.matcher(str).replaceAll("\\\\$0");
}

Then you can use it like this, for example:

Pattern toSafePattern(String text)
{
    return Pattern.compile(".*" + escapeSpecialRegexChars(text) + ".*");
}

We needed to do that because, after escaping, we add some regex expressions. If not, you can simply use \Q and \E:

Pattern toSafePattern(String text)
{
    return Pattern.compile(".*\\Q" + text + "\\E.*")
}
2 of 8
41

Is there any method in Java or any open source library for escaping (not quoting) a special character (meta-character), in order to use it as a regular expression?

If you are looking for a way to create constants that you can use in your regex patterns, then just prepending them with "\\" should work but there is no nice Pattern.escape('.') function to help with this.

So if you are trying to match "\\d" (the string \d instead of a decimal character) then you would do:

// this will match on \d as opposed to a decimal character
String matchBackslashD = "\\\\d";
// as opposed to
String matchDecimalDigit = "\\d";

The 4 slashes in the Java string turn into 2 slashes in the regex pattern. 2 backslashes in a regex pattern matches the backslash itself. Prepending any special character with backslash turns it into a normal character instead of a special one.

matchPeriod = "\\.";
matchPlus = "\\+";
matchParens = "\\(\\)";
... 

In your post you use the Pattern.quote(string) method. This method wraps your pattern between "\\Q" and "\\E" so you can match a string even if it happens to have a special regex character in it (+, ., \\d, etc.)

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-check-whether-the-string-consists-of-special-characters
Java Program to Check Whether the String Consists of Special Characters - GeeksforGeeks
July 23, 2025 - // Java Program to Check Whether String contains Special // Characters using Regex classes import java.util.regex.Matcher; import java.util.regex.Pattern; public class GFG { public static void main(String[] args) { String s1 = "GeeksForGeeks"; // Creating regex pattern by // creating object of Pattern class Pattern p = Pattern.compile( "[^a-z0-9 ]", Pattern.CASE_INSENSITIVE); // Creating matcher for above // pattern on our string Matcher m = p.matcher(s1); // Now finding the matches for which // let us set a boolean flag and // imposing find() method boolean res = m.find(); // If special chara