简体   繁体   English

Java正则表达式匹配\\ n \\ r和\\ t以外的不可打印字符

[英]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: 我是Regex大厦的绝对入门者,但遇到一个问题:

User input sometimes contains unusual chars like "xA0" character, and I need it removed. 用户输入有时包含不寻常的字符,例如“ xA0”字符,我需要将其删除。

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?? 但是它匹配换行符“ \\ n”(我认为它匹配\\ r和\\ t)是否有办法从匹配器中排除那些字符?

Thx in advance :-) 提前谢谢:-)

Use a negated char class. 使用否定的char类。

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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