- 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.
Top answer 1 of 10
117
- 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.
2 of 10
103
You can look at the javadoc of the Pattern class: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
You need to escape any char listed there if you want the regular char and not the special meaning.
As a maybe simpler solution, you can put the template between \Q and \E - everything between them is considered as escaped.
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
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
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
delete all special characters at start and end of string
If you just need to remove single quotes from start/end of string, you can use s.strip("'") If you want to remove all non-alphanumeric characters from start/end, try re.sub(r'\A[^a-z\d]+|[^a-z\d]+\Z', '', s, flags=re.I) More on reddit.com
The Regex in Java | Complete Java Course for Beginners #18
09:22
RegEx Beginner Tutorial - How to Match Special Characters (Email ...
00:59
Remove Special Characters from a String using Regular Expression ...
13:38
REGEX SPECIAL CHARACTERS Simplified | Part 8 - YouTube
12:17
Special Characters In Java | Java Full Course From Scratch - YouTube
07:36
Regular Expressions (RegEx) Tutorial #7 - Special Characters - YouTube
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.
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 ...
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!!!).
MojoAuth
mojoauth.com › escaping › regex-escaping-in-java
Regex Escaping in Java | Escaping Methods in Programming Languages
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 \.. Implementing regex escaping in Java is straightforward.
Top answer 1 of 6
18
This worked for me:
String result = str.replaceAll("[^\\dA-Za-z ]", "").replaceAll("\\s+", "+");
For this input string:
/-+!@#$%^&())";:[]{}\ |wetyk 678dfgh
It yielded this result:
+wetyk+678dfgh
2 of 6
7
replaceAll expects a regex:
public static final String specialChars2 = "[`~!@#$%^&*()_+[\\]\\\\;\',./{}|:\"<>?]";
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 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.
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - 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.
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
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 - We can use Regular expressions to check special characters. The regex [^a-zA-Z0-9 ] matches any character that is not a letter, digit, or space.
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).
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.
TutorialsPoint
tutorialspoint.com › article › moving-all-special-char-to-the-end-of-the-string-using-java-regular-expression-regex
Moving all special char to the end of the String using Java Regular Expression RegEx)
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String args[]) { String input = "sample # text * with & special@ characters"; String regex = "[^a-zA-Z0-9\s+]"; String specialChars = ""; System.out.println("Input string: \n"+input); //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { specialChars = specialChars+matcher.group(); matcher.appendReplacement(sb, ""); } matcher.appendTail(sb); System.out.println("Result: \n"+ sb.toString()+specialChars ); } }
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
It covers basic regex syntax, Java’s Pattern and Matcher classes, practical examples for common use cases, and important security considerations. A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special ...