replace characters which do not match with the ones in a regex
- by Cristian Boariu
Hi,
I have this regex:
private static final String SPACE_PATH_REGEX ="[a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+";
I check if my string matches this regex and IF NOT, i want to replace all characters which are not here, with "_".
I've tried like:
private static final String SPACE_PATH_REGEX_EXCLUDE =
"[~a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+";
if (myCompanyName.matches(SPACE_PATH_REGEX)) {
myNewCompanySpaceName = myCompanyName;
} else{
myNewCompanySpaceName = myCompanyName.replaceAll(
SPACE_PATH_REGEX_EXCLUDE, "_");
}
but it does not work..., so in the 2nd regex "~" seems to not omit the following chars.
Any idea?