I have this line of code to remove some punctuation:
str.replaceAll("[\\-\\!\\?\\.\\,\\;\\:\\\"\\']", "");
I don’t know if all the chars in this regex need to be escaped, but I escaped only for safety.
Is there some way to build a regex like this in a more clear way?
Inside
[…]you don’t need to escape the characters.[.]for instance wouldn’t make sense anyway!The exceptions to the rule are
]since it would close the whole[...]expression prematurely.^if it is the first character, since[^abc]matches everything exceptabc.-unless it’s the first/last character, since[a-z]matches all characters betweenatoz.Thus, you could write
To quote a string into a regular expression, you could also use
Pattern.quotewhich escapes the characters in the string as necessary.Demo: