I have this string 'Enterprise - SELA'; And I want to convert it to 'Enterprise_SELA'; I can do this with the regex: \W+


 original.replace('\W+', '_') 

throws the

Illegal character sequence \W'

error and

original.replace('\\W+', '_') 

doesn't carry out the replacement. Nor does original.replace('\\W+', '_')

1 Answer 1


You need to use String.replaceAll, not String.replace. String.replace only looks for a literal string.

String original = 'Enterprise - SELA';
String replaced = original.replaceAll('\\W+', '_');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .