• 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
🌐
Delft Stack
delftstack.com › home › howto › java › java regex special charcters
How to Handle Regex Special Characters in Java | Delft Stack
February 14, 2024 - 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.
🌐
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 - This means that in the previous example, we don’t want to let the pattern foo. to have a match in the input String. How would we handle a situation like this? The answer is that we need to escape the dot (.) character so that its special meaning is ignored. Let’s dig into it in more detail in the next section. According to the Java API documentation for regular expressions, there are two ways in which we can escape characters that have special meaning.
🌐
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 requires literal opening braces to be escaped.
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
Similarly, *, +, ? (occurrence indicators), ^, $ (position anchors) have special meaning in regex. You need to use an escape code to match with these characters. (gif|png|jpg|jpeg) matches either "gif", "png", "jpg" or "jpeg". The | denotes "OR" operator. The parentheses are used for grouping ...
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
May 31, 2026 - For example, the following will match "a" if "a" is not followed by "b". ... You can add the mode modifiers to the start of the regex. To specify multiple modes, put them together as in (?ismx). ... (?m) for "multi-line mode" makes the caret and dollar match at the start and end of each line in the subject string. The backslash \ is an escape character in Java Strings.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-illustrate-escaping-characters-in-regex
Java Program to Illustrate Escaping Characters in Regex - GeeksforGeeks
September 30, 2021 - Generally used for escaping multiple characters. In the below source code the Regex pattern p is escaped for the dot(.) operator, whereas the pattern p1 is not escaped for dot(.). Thus, the pattern p matches only with the string s whereas the pattern p1 matches with both the strings s and s1. ... // Java Program to Illustrate Escaping Characters in Java // Regex Using \Q and \E for escaping // Importing required classes import java.io.*; import java.util.regex.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Sample strings as inputs String s = "Geeks.forGeeks"; String s1 = "GeeksforGeeks"; // Creating object of Pattern class // 1.
🌐
How to do in Java
howtodoinjava.com › home › java regular expressions › regex meta characters (with examples)
Regex Meta Characters (with Examples)
May 26, 2024 - For example, \\. matches a literal dot (‘.’) character. // Matches the literal dot character Pattern pattern = Pattern.compile("\\."); Matcher matcher = pattern.matcher("1.2.3"); while (matcher.find()) { System.out.println(matcher.group()); } In this Java regex example, we learned to use meta characters in regular expressions to evaluate text strings.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 7 )
Backslashes within string literals in Java source code are interpreted as required by The Java™ Language Specification as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6) It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary.
🌐
CodingTechRoom
codingtechroom.com › question › java-regex-special-characters-escape
What Special Characters Must Be Escaped in Java Regex? - CodingTechRoom
In Java regex, certain characters are considered special and need to be escaped to ensure they are treated as literal characters rather than as regex controls. Below is a comprehensive list of these characters along with a universal method for escaping them in Java. ... // Example of escaping special characters in a string String input = "Hello (world)!"; String escaped = Pattern.quote(input); // Result: "Hello\ (world\)!"
🌐
MojoAuth
mojoauth.com › escaping › regex-escaping-in-java
Regex Escaping in Java | Escaping Methods in Programming Languages
In Java, regex escaping involves using a backslash (``) before a special character to indicate that it should be treated literally. Common special characters that often require escaping include: ... To escape these characters in Java, you would typically use a double backslash (\) within a string literal because the backslash is also an escape character in Java strings. For example, to match a literal dot, you would write \..
🌐
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
Compile the program using the javac command in the Terminal: ... Original String: Hello! This is a test string with some special characters: @#$%^&*()_+ Regex Pattern: [^a-zA-Z0-9\s] Special characters found: !
🌐
javaspring
javaspring.net › blog › java-regular-expression-to-search-a-list-of-special-characters
Mastering Java Regular Expressions for Searching Special Characters — javaspring.net
The Pattern and Matcher classes are the core components for working with regular expressions in Java. Here is a simple example of searching for a list of special characters in a string: import java.util.regex.Matcher; import java.util.regex.Pattern; public class SpecialCharacterSearch { public static void main(String[] args) { String input = "Hello!
🌐
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 ...
🌐
Oracle
oracle.com › java › technical details
Regular Expressions and the Java Programming Language
There are many special characters used in regular expressions to find words at the beginning of lines, words that ignore case or are case-specific, and special characters that give a range, such as a-e, meaning any letter from a to e. Regular expression usage using this new package is Perl-like, ...
🌐
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 - For instance, regular expressions (regexes) can be used in queries to find all items in which any frame, or a specific frame, or any of a list of frames, contains text matching the regular expression that you are searching for. For example, if you wanted to find all items containing sequences of capital letters followed by numbers, then the regular expression would be: ... Finished\? matches “Finished?” ^http matches strings that begin with http [^0-9] matches any character not 0-9 ing$ matches “exciting” but not “ingenious” gr.y matches “gray“, “grey” Red|Yellow matches “Red” or “Yellow” colou?r matches colour and color Ah?
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
Think of them as sets, if a character in some text belongs to the character class, it is matched. Here is a table with the most used character classes in Java Regex. ... For your convenience, there are some useful classes defined already. For example, digits are a perfect example of a useful ...