简体   繁体   中英

Java regex match non printable characters except \n \r and \t

I am an absolute beginner in Regex building but i came across a problem:

User input sometimes contains unusual chars like "xA0" character, and I need it removed.

I tried with

string.replaceAll("\\P{Print}", "");

But it matches new line "\\n" character (and i think it matches \\r and \\t) Is there a way to exclude those character from matcher??

Thx in advance :-)

Use a negated char class.

string.replaceAll("[^\\n\\r\\t\\p{Print}]", "");

or

string.replaceAll("(?![\\n\\r\\t])\\P{Print}", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM