我有一个程序,可以实时解析来自Twitter流API的推文。在存储它们之前,我将它们编码为utf8。某些字符以?、??或???的形式出现在字符串中,而不是它们各自的Unicode代码,并引起问题。经过进一步调查,我发现有问题的字符来自于“表情符号”块,U+1F600 - U+1F64F,以及“杂项符号和象形文字”块,U+1F300 - U+1F5FF。我尝试了删除,但未成功,因为匹配器最终替换了字符串中几乎每个字符,而不仅仅是我所需的Unicode范围。
String utf8tweet = "";
try {
byte[] utf8Bytes = status.getText().getBytes("UTF-8");
utf8tweet = new String(utf8Bytes, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Pattern unicodeOutliers = Pattern.compile("[\\u1f300-\\u1f64f]", Pattern.UNICODE_CASE | Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE);
Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);
utf8tweet = unicodeOutlierMatcher.replaceAll(" ");
我该怎么做才能去除这些字符?