replace characters which do not matches 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:13 UTC
Read the original article Hit count: 287

Filed under:
|

Hi, I have this regex:

private static final String SPACE_PATH_REGEX ="[a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+";

I check if my string matches these 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 does not work..., so in the 2nd regex "~" seems to not omit the following chars.

Any ideea?

© Stack Overflow or respective owner

Related posts about regex

Related posts about java