正则表达式的一些常用场景
(1)过滤表情符号
- /***
- * 过滤表情符号<br>
- * see:http://blog.csdn.net/beatrice_g20/article/details/48489437
- *
- */
- public static String filterExpression(String str) {
- if (ValueWidget.isNullOrEmpty(str)) {
- return str;
- }
- String pattern = "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
- Pattern emoji = Pattern.compile(pattern);
- Matcher emojiMatcher = emoji.matcher(str);
- str = emojiMatcher.replaceAll(SystemHWUtil.EMPTY);
- return str;
- }
应用:下载
- String anser = convention.getAnswer();
- if (!ValueWidget.isNullOrEmpty(anser)) {
- anser = RegexUtil.filterExpression(anser);
- convention.setAnswer(anser);
- }
(2)删除重复的行(必须是相连的)
- /***
- * 删除重复的行(必须是相连的)
- */
- public static String deleteDuplicateRow(String input) {
- if (ValueWidget.isNullOrEmpty(input)) {
- return input;
- }
- String[] rows = input.split(SystemHWUtil.CRLF);
- List<String> stringlist = new ArrayList<>();
- for (int i = 0; i < rows.length; i++) {
- stringlist.add(rows[i]);
- }
- // 搜索结果是经过排序的,根据此规律删除不合要求File
- for (int i = 0; i < stringlist.size() - 1; i++) {
- if (stringlist.get(i).equals(stringlist.get(i + 1))) {
- stringlist.remove(i);
- if (i != 0) i--;
- }
- }
- return SystemHWUtil.formatArr(stringlist, SystemHWUtil.CRLF);
- }
应用下载:
- System.out.println("input:"+input);
- String unicodeStr = 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)驼峰标识
- /***
- * 驼峰标识<br>
- *
- */
- public static String humpMarking(String input){
- Pattern p=Pattern.compile("_[a-z]");
- Matcher m=p.matcher(input);
- StringBuffer sb = new StringBuffer();
- boolean result=m.find();
- while(result){
- String findResult=m.group().toUpperCase().replaceAll("^_", SystemHWUtil.EMPTY);
- m.appendReplacement(sb, findResult);
- result=m.find();
- }
- m.appendTail(sb);
- return sb.toString().replaceAll("_$", SystemHWUtil.EMPTY);
- }
(4)把换行的段落当做字符串拼接
- /***
- * 把换行的段落当做字符串拼接
- */
- public static String splitPlus(String input, String cr, String quote, boolean keepBlank) {
- input=input.replaceAll(cr+"+$","");
- String chinese;
- // String quote="\"";
- String replacement = quote + " + SystemHWUtil.CRLF +$1" + quote + "$2";
- String regex = null;
- String regexMutip = 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);
- }else if(cr.equals("\\n")){
- chinese = input.replaceAll("(\n)" + regexMutip + regex, replacement);
- }else if(cr.equals("\\r")){
- chinese = input.replaceAll("(\r)" + regexMutip + regex, replacement);
- }else{
- chinese = input.replaceAll("(\n\r)" + regexMutip + regex, replacement);
- }
- return quote + chinese + quote + " ;";
- }
应用下载:
- public static String splitPlus(String input, String cr, String quote) {
- return splitPlus(input, cr, quote, false);
- }
- @Override
- public String callbackAdditional(String input, Object crfl) {
- return RegexUtil.splitPlus(input, (String) crfl, "\"", true);
- }
(5)是否包含指定字符串,不区分大小写
- /***
- * 是否包含指定字符串,不区分大小写
- *
- */
- public static boolean contain2(String input, String regex) {
- /***
- * input:(1)bss登录(2) <br>
- regex:bss登录(2)
- */
- regex = regex.replace("(", "\\(");
- regex = regex.replace(")", "\\)");
- if (ValueWidget.isNullOrEmpty(input)) {
- return false;
- }
- Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(input);
- boolean result = m.find();
- return result;
- }