删除所有不可打印的字符

删除所有不可打印的字符

本文介绍了正则表达式,删除所有不可打印的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从字符串中删除所有不可打印的ASCII字符,同时保留不可见的ASCII字符.我以为这会行得通,因为空格\ n \ r是不可见的字符,但不是不可打印的吗?基本上,我得到了一个带有``.''字符的字节数组,我不希望它们出现在其中.因此,我尝试将其转换为字符串,删除 字符,然后再次将其用作字节数组.

I wish to remove all non-printable ascii characters from a string while retaining invisible ones. I thought this would work because whitespace, \n \r are invisible characters but not non-printable?Basically I am getting a byte array with � characters in it and I don't want them to be in it. So i am trying to convert it to a string, remove the � characters before using it as a byte array again.

空格现在在我的代码中可以正常工作,但是现在\ r和\ n不起作用.保留这些正则表达式的正确方法是什么?还是我在做什么更好的方法?

Space works fine in my code now, however now \r and \n do not work. What would be the correct regex to retain these also? Or is there a better way that what I am doing?

public void write(byte[] bytes, int offset, int count) {

    try {
        String str = new String(bytes, "ASCII");
        str2 = str.replaceAll("[^\\p{Print}\\t\\n]", "");
        GraphicsTerminalActivity.sendOverSerial(str2.getBytes("ASCII"));

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    }

     return;
 }

}

我尝试了[^ \ x00- \ x7F]这是ascii字符的范围....但是 符号仍然可以通过,很奇怪.

I tried [^\x00-\x7F] which is the range of ascii characters....but then the � symbols still get through, weird.

推荐答案

以下正则表达式仅与可打印文本匹配

The following regex will only match printable text

[^\x00\x08\x0B\x0C\x0E-\x1F]*

以下正则表达式将查找不可打印的字符

The following Regex will find non-printable characters

[\x00\x08\x0B\x0C\x0E-\x1F]

Jave代码:

boolean foundMatch = false;
try {
    Pattern regex = Pattern.compile("[\\x00\\x08\\x0B\\x0C\\x0E-\\x1F]");
    Matcher regexMatcher = regex.matcher(subjectString);
    foundMatch = regexMatcher.find();
    //Relace the found text with whatever you want
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

这篇关于正则表达式,删除所有不可打印的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:15