Replace any sequence of non-letters with a single white space:
str.replaceAll("[^a-zA-Z]+", " ")
You also might want to apply trim() after the replace.
If you want to support languages other than English, use "[^\\p{IsAlphabetic}]+" or "[^\\p{IsLetter}]+". See this question about the differences.
Replace any sequence of non-letters with a single white space:
str.replaceAll("[^a-zA-Z]+", " ")
You also might want to apply trim() after the replace.
If you want to support languages other than English, use "[^\\p{IsAlphabetic}]+" or "[^\\p{IsLetter}]+". See this question about the differences.
The OR operator (|) should work:
System.out.println(str.replaceAll("([^a-zA-Z]|\\s)+", " "));
Actually, the space doesn't have to be there at all:
System.out.println(str.replaceAll("[^a-zA-Z]+", " "));
You can remove everything but the digits:
phone = phone.replaceAll("[^0-9]","");
To remove all non-digit characters you can use
replaceAll("\\D+",""); \\ \D is negation of \d (where \d represents digit)
If you want to remove only spaces, ( and ) you can define your own character class like
replaceAll("[\\s()]+","");
Anyway your problem was caused by fact that some of characters in regex are special. Among them there is ( which can represent for instance start of the group. Similarly ) can represent end of the group.
To make such special characters literals you need to escape them. You can do it many ways
"\\("- standard escaping in regex"[(]"- escaping using character class"\\Q(\\E"-\Qand\Ecreate quote - which means that regex metacharacters in this area should be treated as simple literalsPattern.quote("("))- this method usesPattern.LITERALflag inside regex compiler to point that metacharacters used in regex are simple literals without any special meaning
That depends on what you define as special characters, but try replaceAll(...):
String result = yourString.replaceAll("[-+.^:,]","");
Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".
Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).
So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):
String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");
If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").
A third way could be something like this, if you can exactly define what should be left in your string:
String result = yourString.replaceAll("[^\\w\\s]","");
This means: replace everything that is not a word character (a-z in any case, 0-9 or _) or whitespace.
Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.
Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:
String result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");
The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.
Additional information on Unicode
Some unicode characters seem to cause problems due to different possible ways to encode them (as a single code point or a combination of code points). Please refer to regular-expressions.info for more information.
This will replace all the characters except alphanumeric
replaceAll("[^A-Za-z0-9]","");
st.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).
st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.
The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.
Assign the value to a variable, if not used directly:
st = st.replaceAll("\\s+","")
replaceAll("\\s","")
\w = Anything that is a word character
\W = Anything that isn't a word character (including punctuation etc)
\s = Anything that is a space character (including space, tab characters etc)
\S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)
(Edit: As pointed out, you need to escape the backslash if you want \s to reach the regex engine, resulting in \\s.)