Will
Will

Reputation: 8631

How to remove all non alphanumeric chars from a String without remove @ symbols

currently using this regex: s.replaceAll("[^a-zA-Z0-9]", ""); however it removes values I need such as @ . , ! how can I include these in my regex so they do not get replaced with empty strings.

Cheers

Upvotes: 2

Views: 2972

Answers (1)

Mureinik
Mureinik

Reputation: 311338

Just add them to the regex. Note that . is a special character, so you'd have to escape it:

String newString = s.replaceAll("[^a-zA-Z0-9!@\\.,]", "");

Upvotes: 4

Related Questions