Home  >  Article  >  Java  >  Java filters garbled characters (\u format garbled characters, unicode garbled characters)

Java filters garbled characters (\u format garbled characters, unicode garbled characters)

尚
Original
2019-12-14 09:30:223243browse

Java filters garbled characters (\u format garbled characters, unicode garbled characters)

由于编辑人员从excel,word等乱七八糟的地方copy内容过来,其中有不可见的字符,导致输出内容看上去是对的,其实是多了一个零长度的字符(比如:\u2028,0000200B ZERO WIDTH SPACE),所以需要过滤掉不合法的unicode编码等特殊字符

整理的正则:

[\\u007f-\\u009f]|\\u00ad|[\\u0483-\\u0489]|[\\u0559-\\u055a]|\\u058a|[\\u0591-\\u05bd]|\\u05bf|[\\u05c1-\\u05c2]|[\\u05c4-\\u05c7]|[\\u0606-\\u060a]|[\\u063b-\\u063f]|\\u0674|[\\u06e5-\\u06e6]|\\u070f|[\\u076e-\\u077f]|\\u0a51|\\u0a75|\\u0b44|[\\u0b62-\\u0b63]|[\\u0c62-\\u0c63]|[\\u0ce2-\\u0ce3]|[\\u0d62-\\u0d63]|\\u135f|[\\u200b-\\u200f]|[\\u2028-\\u202e]|\\u2044|\\u2071|[\\uf701-\\uf70e]|[\\uf710-\\uf71a]|\\ufb1e|[\\ufc5e-\\ufc62]|\\ufeff|\\ufffc

java代码如下:

private String replaceWrongUnicode(String source, String replace) {
        if (StringUtils.isBlank(source)) {
            return source;
        }
        if (StringUtils.isBlank(replace)) {
            replace = "";
        }
        Pattern CRLF = Pattern.compile("([\\u007f-\\u009f]|\\u00ad|[\\u0483-\\u0489]|[\\u0559-\\u055a]|\\u058a|[\\u0591-\\u05bd]|\\u05bf|[\\u05c1-\\u05c2]|[\\u05c4-\\u05c7]|[\\u0606-\\u060a]|[\\u063b-\\u063f]|\\u0674|[\\u06e5-\\u06e6]|\\u070f|[\\u076e-\\u077f]|\\u0a51|\\u0a75|\\u0b44|[\\u0b62-\\u0b63]|[\\u0c62-\\u0c63]|[\\u0ce2-\\u0ce3]|[\\u0d62-\\u0d63]|\\u135f|[\\u200b-\\u200f]|[\\u2028-\\u202e]|\\u2044|\\u2071|[\\uf701-\\uf70e]|[\\uf710-\\uf71a]|\\ufb1e|[\\ufc5e-\\ufc62]|\\ufeff|\\ufffc)");
        Matcher m = CRLF.matcher(source);
        if (m.find()) {
            return m.replaceAll(replace);
        }
        return source;
    }

附:过滤\n成076402276aae5dbec7f672f8f4e5cc81

private String replaceEnter(String source) {
        if (StringUtils.isBlank(source)) {
            return source;
        }
        Pattern CRLF = Pattern.compile("(\r\n|\r|\n|\n\r)");
        Matcher m = CRLF.matcher(source);
        if (m.find()) {
            return m.replaceAll("<br/>");
        }
        return source;
    }

更多java知识请关注java基础教程栏目。

The above is the detailed content of Java filters garbled characters (\u format garbled characters, unicode garbled characters). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn