非英文字符删除
下面的代码用来去除非英文字符。例如将汉字、数字等字符全部去除
public static String
isEnglishWord(String
[] authors
) {
ArrayList
<String> words
= new ArrayList<>();
FileUtils
.getStringArrList(authors
,words
);
for(int i
= 0; i
< words
.size(); i
++){
if(isContainNoEnglish(words
.get(i
))){
words
.remove(i
);
i
--;
}
}
String text
= FileUtils
.getRecombine(words
);
return text
;
}
public static boolean isContainNoEnglish(String str
) {
Pattern p
= Pattern
.compile("[^a-zA-Z\\s]");
Matcher m
= p
.matcher(str
);
if (m
.find()) {
return true;
}
return false;
}
非法字符删除
即只保留文本中的汉字、数字以及英文字符,其他字符删除。 为实现这个目标,只需将isContainNoEnglish()方法中的正则表达式改为:
[^A-Za-z0-9 \\u4e00-\\u9fa5]