• 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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
April 21, 2026 - Unicode escape sequences such as \u2014 in Java source code are processed as described in section 3.3 of The Java™ Language Specification. Such escape sequences are also implemented directly by the regular-expression parser so that Unicode escapes can be used in expressions that are read from files or from the keyboard. Thus the strings "\u2014" and "\\u2014", while not equal, compile into the same pattern, which matches the character with hexadecimal value 0x2014.
Discussions

RegexMatcher for Special Characters
I want to identify if there are any invalid characters in my records. Only below special characters are valid "[a-zA-Z][0-9]^_=!#$%&()*+,-.:'/?@ ") If any other special character appears other than above, it needs to say false. Eg: In the above special characters, there is no “|,~”. If ... More on forum.knime.com
🌐 forum.knime.com
1
0
October 6, 2023
Regex for all special characters
Do the special characters need to be separated by spaces when put into the list? Or, can they be one after the other like that · I would encourage you to try out a test site as well where you can paste in your regex and test strings and make sure they trigger as expected. More on community.spiceworks.com
🌐 community.spiceworks.com
4
1
June 8, 2024
Escape all special characters in a string
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
17
2
August 29, 2023
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
🌐
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.
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
A regular expression can be a single character, or a more complicated pattern. Regular expressions can be used to perform all types of text search and text replace operations. Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
You need to escape it too, so be prepared to see something like "\\\\" in the regex code.Back to top · On top of specifying the expressions that contain individual characters only, you can define the whole classes of characters. 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.
🌐
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!!!).
Find elsewhere
🌐
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 ...
🌐
FormulasHQ
formulashq.com › home › blog › escaped characters: regular expressions regex explained
Escaped Characters: Regular Expressions REGEX Explained — FormulasHQ
June 17, 2024 - There are several common escaped characters in REGEX. These include \., \\, \+, \*, \?, \^, \$, \(, \), \[, \], \{, and \}. Each of these characters has a special meaning within a regular expression, and escaping them allows for their literal use.
🌐
QBasic on Your Computer
chortle.ccsu.edu › finiteautomata › Section07 › sect07_12.html
Basic Regular Expressions: Exclusions
except a list of excluded characters, put the excluded charaters between [^ and ]. The caret ^ must immediately follow the [ or else it stands for just itself.
🌐
Coderanch
coderanch.com › t › 487300 › java › validation-Special-character-Regular-Expressions
validation Special character using Regular Expressions (Java in General forum at Coderanch)
March 15, 2010 - So, if you want the regex to not treat the backslash as special, and you want to use a string literal, then you need to escape twice.... giving you four backslashes. Henry · Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
🌐
Jenkov
jenkov.com › tutorials › java-regex › index.html
Java Regex - Java Regular Expressions
March 5, 2019 - As mentioned above, metacharacters in Java regular expressions have a special meaning. If you really want to match these characters in their literal form, and not their metacharacter meaning, you must "escape" the metacharacer you want to match. To escape a metacharacter you use the Java regular expression escape character - the backslash character.
🌐
LeetCode
leetcode.com › problems › regular-expression-matching
Regular Expression Matching - LeetCode
Can you solve this real interview question? Regular Expression Matching - Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: * '.' Matches any single character. * '*' Matches zero or more of the preceding element.
🌐
Baeldung
baeldung.com › home › java › java string › regular expressions s and s+ in java
Regular Expressions \s and \s+ in Java | Baeldung
January 8, 2024 - Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › regex › char_classes.html
Character Classes (The Java™ Tutorials > Essential Java Classes > Regular Expressions)
Enter your regex: [^bcr]at Enter input string to search: hat I found the text "hat" starting at index 0 and ending at index 3. The match is successful only if the first character of the input string does not contain any of the characters defined by the character class.
🌐
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
Special characters in regex have specific meanings and are used to define patterns. ... For example, if we want to match any digit, we can use \d. If we want to match any non-digit, we use \D. Similarly, \s matches any whitespace character, ...
🌐
Coderanch
coderanch.com › t › 385862 › java › Removing-special-characters-String
Removing special characters from a String (Java in General forum at Coderanch)
July 24, 2008 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hello everyone, I am reading a string from a JTextArea which contains special characters such as " and , and \etc.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-illustrate-escaping-characters-in-regex
Java Program to Illustrate Escaping Characters in Regex - GeeksforGeeks
September 30, 2021 - Special Characters like dot(.), ... have a special meaning to the regular expression need to be escaped to match in the regular expression. For example, if dot(.) is not escaped in a regular expression, it matches any single character, thus ...
🌐
Mendix
community.mendix.com › link › spaces › app-development › questions › 91044
Regular expression which doesnt allow special Characters
August 28, 2018 - Do we have any regular expression to be used in microflow which doesn't allow special characters and throw validation ... I usually check if my string only has characters which are valid. In this case you can use: ... you can store this regex in a string variable and perform isMatch function on the string.