Java正则表达式特殊字符及其处理以及正则表达式详解
image.png
// 清除掉所有特殊字符
String regEx = ".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]+.*";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.matches();
}
public static String filterString(String str) throws PatternSyntaxException {
String regEx= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("_").trim();
}
public void setContent(String content) {
// 格式化字符串(替换特殊符号)
String text = null;
// 设置子View水平居中
setGravity(Gravity.CENTER_HORIZONTAL);
Pattern pattern = Pattern.compile(imageRegex);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
// 加载文字
text = content.substring(startPos, matcher.start());
if (!TextUtils.isEmpty(text)) {
appendTextView(clearNewlineChar(text));
}
// 加载图片
appendImageView(content.substring(matcher.start() + 5, matcher.end() - 6));
startPos = matcher.end();
}
// 加载最后一个图片后面的文字
text = content.substring(startPos);
if (!TextUtils.isEmpty(text)) {
appendTextView(clearNewlineChar(text));
}
}
网友评论