replace characters which do not match with the ones in a regex
Posted
by Cristian Boariu
on Stack Overflow
See other posts from Stack Overflow
or by Cristian Boariu
Published on 2010-04-09T11:07:33Z
Indexed on
2010/04/09
11:23 UTC
Read the original article
Hit count: 405
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?
© Stack Overflow or respective owner