正则表达式的一些常用场景
(1)过滤表情符号
Java代码
/***
*过滤表情符号<br>
*see:http://blog.csdn.net/beatrice_g20/article/details/48489437
*
*/
publicstaticStringfilterExpression(Stringstr){
if(ValueWidget.isNullOrEmpty(str)){
returnstr;
}
Stringpattern="[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
Patternemoji=Pattern.compile(pattern);
MatcheremojiMatcher=emoji.matcher(str);
str=emojiMatcher.replaceAll(SystemHWUtil.EMPTY);
returnstr;
}
应用:下载
Java代码
Stringanser=convention.getAnswer(); if(!ValueWidget.isNullOrEmpty(anser)){ anser=RegexUtil.filterExpression(anser); convention.setAnswer(anser); } (2)删除重复的行(必须是相连的) Java代码*删除重复的行(必须是相连的) staticStringdeleteDuplicateRow(Stringinput){ if(ValueWidget.isNullOrEmpty(input)){ returninput; } String[]rows=input.split(SystemHWUtil.CRLF); List<String>stringlist=newArrayList<>(); for(inti=0;i<rows.length;i++){ stringlist.add(rows[i]); } //搜索结果是经过排序的,根据此规律删除不合要求File 0;i<stringlist.size()-1;i++){ if(stringlist.get(i).equals(stringlist.get(i+1))){ stringlist.remove(i); if(i!=0)i--; } } returnSystemHWUtil.formatArr(stringlist,SystemHWUtil.CRLF); } 应用:下载 Java代码
System.out.println("input:"+input); StringunicodeStr=input.replace("\"3e[\r][\n]\"","").replaceAll("\\[[\\s]*0[xX][\\s]*([\\w]{2})\\]","%$1") .replaceAll("\\[0[xX]([\\w]{1})\\]","%0$1"); //ComponentUtil.appendResult(resultTextArea,unicodeStr,true,false); unicodeStr=RegexUtil.dealCiaResponseRequest(unicodeStr,true); //unicodeStr=RegexUtil.dealCiaResponse(unicodeStr,Constant2.REGEX_DELETE_FRONT_OF_CIA,true); //删除重复的行(必须是相连的) unicodeStr=RegexUtil.deleteDuplicateRow(unicodeStr); (3)驼峰标识 Java代码*驼峰标识<br> * staticStringhumpMarking(Stringinput){ Patternp=Pattern.compile("_[a-z]"); Matcherm=p.matcher(input); StringBuffersb=newStringBuffer(); booleanresult=m.find(); while(result){ StringfindResult=m.group().toUpperCase().replaceAll("^_",SystemHWUtil.EMPTY); m.appendReplacement(sb,findResult); result=m.find(); } m.appendTail(sb); returnsb.toString().replaceAll("_$",SystemHWUtil.EMPTY); } (4)把换行的段落当做字符串拼接 Java代码下载*把换行的段落当做字符串拼接 staticStringsplitPlus(Stringinput,Stringcr,Stringquote,85);font-weight:bold;">booleankeepBlank){ input=input.replaceAll(cr+"+$",""); Stringchinese; //Stringquote="\""; Stringreplacement=quote+"+SystemHWUtil.CRLF+$1"+quote+"$2"; Stringregex=null; StringregexMutip=null;//是否匹配多个换行 if(keepBlank){//保持空格<br/>[^\n\r]表示不是换行 regex="([^\n\r]*)"; regexMutip=""; }else{ regex="[\\s]*([^\\s]+.*)"; regexMutip="+"; } input=input.replace(quote,"\\"+quote);//原字符串转义 //简单点说,就是把[换行]替换为['(单引号)+(加号)'(单引号)] //所以最前面和最后面需要各补一个单引号 if(cr.equals("\\r\\n")){ chinese=input.replaceAll("(\r\n)"+regexMutip+regex,replacement); }elseif(cr.equals("\\n")){ chinese=input.replaceAll("(\n)"+regexMutip+regex,85);font-weight:bold;">if(cr.equals("\\r")){ chinese=input.replaceAll("(\r)"+regexMutip+regex,85);font-weight:bold;">else{ chinese=input.replaceAll("(\n\r)"+regexMutip+regex,replacement); } returnquote+chinese+quote+";"; } 应用: Java代码
returnsplitPlus(input,cr,quote,85);font-weight:bold;">false); } @Override publicStringcallbackAdditional(Stringinput,Objectcrfl){ returnRegexUtil.splitPlus(input,(String)crfl,"\"",85);font-weight:bold;">true); } (5)是否包含指定字符串,不区分大小写 staticbooleancontain2(Stringinput,Stringregex){ *input:(1)bss登录(2)<br> regex:bss登录(2) */ regex=regex.replace("(","\\("); regex=regex.replace(")","\\)"); returnfalse; } Patternp=Pattern.compile(regex,Pattern.CASE_INSENSITIVE); Matcherm=p.matcher(input); returnresult; }