Reputation: 1110
I am trying to create a #replaceAll regex to file out certain characters.
I tried the following
msg.replaceAll("[^\\w~!@#$%^&*()-=_+`[]{}\\|:;'\"<>,./\\]", "");
but it gave me this error
INFO Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 36
07.09 00:07:24 [Server] INFO [^\w~!@#$%^&*()-=_+`[]{}\|:;'"<>,./\]
07.09 00:07:24 [Server] INFO ^
I've tried searching online but don't know exactly what I'm doing wrong.
Upvotes: 0
Views: 83
Reputation: 27723
My guess is that maybe this expression might be desired or close to one:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "[^\\w~!@#$%^&*()=_+`\\[\\]{}|:;'\"<>,.\\\/-]";
final String string = "ábécédééefg";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
}
}
bcdefg
If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
Upvotes: 1
Reputation: 13506
For your regex expression,you have add \\
before the last ]
and do not escape for the first [
,and also you need to escape -
,after changing it to
[^\w~!@#$%^&*()\-=_+`\[\]{}\|:;'\"<>,./]
it works fine in my side
msg = msg.replaceAll("[^\\w~!@#$%^&*()\\-=_+`\\[\\]{}\\|:;'\\\"<>,./]", "");
Upvotes: 1