Java 正则表达式常用惯例
1,判断字符串是否是HTML页面
/*** * 判断是否是HTML 网页 * @param input * @return */ public static boolean isHTMLWebPage(String input){ if(ValueWidget.isNullOrEmpty(input)){ return false; } return input.matches(".*<html[\\s\\w'\"=]*>.*"); }
应用:
@Test public void test_isHTMLWebPage(){ String input="a<html><head><title>"; System.out.println(RegexUtil.isHTMLWebPage(input)); } ComponentUtil.appendResult(resultTextPane, "返回的状态码:" + respCode, true); if(!ValueWidget.isNullOrEmpty(responseJsonResult)&&RegexUtil.isHTMLWebPage(responseJsonResult)){ //弹出框显示HTML CustomDefaultDialog customDefaultDialog=new CustomDefaultDialog(responseJsonResult,"显示HTML",true); customDefaultDialog.setVisible(true); }
2,把换行的段落当做字符串拼接
/*** * 把换行的段落当做字符串拼接 * @param input * @param cr * @param quote * @return */ public static String splitPlus(String input,String cr,String quote){ String chinese; // String quote="\""; String replacement=quote+" +$1"+quote+"$2"; String regex="[\\s]*([^\\s]+.*)"; input=input.replace(quote, "\\"+quote);//原字符串转义 if(cr.equals("\\r\\n")){ chinese=input.replaceAll("(\r\n)"+regex, replacement); }else if(cr.equals("\\n")){ chinese=input.replaceAll("(\n)"+regex, replacement); }else if(cr.equals("\\r")){ chinese=input.replaceAll("(\r)"+regex, replacement); }else{ chinese=input.replaceAll("(\n\r)"+regex, replacement); } return quote+chinese+quote+";"; }
应用:
源代码见附件:
下载地址:http://pan.baidu.com/s/1sjy1ceT
参考:java swing 工具箱:http://hw1287789687.iteye.com/blog/2251439
3,java 模拟linux中sed
/*** * __showLog=True -->__showLog=false * @param source * @param regex : 正则表达式 * @param replacement * @return */ public static String sed(String source,String regex,String replacement){ Pattern p=Pattern.compile(regex,Pattern.MULTILINE); Matcher m=p.matcher(source); String result = m.replaceAll(replacement); return result; } /*** * 模拟linux 的sed 功能 * <br>将关闭输出流 * @param source * @param regex : 正则表达式 * @param replacement * @return */ public static String sed(File source,String regex,String replacement){ String input=null; try { input = FileUtils.getFullContent2(source, SystemHWUtil.CHARSET_UTF); input=sed(input, regex, replacement); FileUtils.writeStrToFile(source, input, false); } catch (IOException e) { e.printStackTrace(); } return input; }
应用:
4,删除每行前面的数字
/*** * 删除每行前面的数字 * @param input * @return */ public static String deleteDigit(String input) { if(ValueWidget.isNullOrEmpty(input)){ return null; } Pattern p = Pattern.compile("^[\\d]+[,:]?[\\s]*",Pattern.MULTILINE); Matcher m = p.matcher(input); String result = m.replaceAll(SystemHWUtil.EMPTY); return result; }
应用:
@Test public void test_deleteDigit(){ String input; try { input = FileUtils.getFullContent3(new File("e:\\tmp\\testdigit.txt"), SystemHWUtil.CHARSET_CURR).toString(); System.out.println(RegexUtil.deleteDigit(input)); } catch (IOException e) { e.printStackTrace(); } }
5,模板sed的参数y
/*** * * @param source * @param arrFrom * @param arrTo * @return */ public static String sedY(String source,String[] arrFrom,String[] arrTo){ if(ValueWidget.isNullOrEmpty(source)){ return SystemHWUtil.EMPTY; } int length=arrFrom.length; for(int i=0;i<length;i++){ String tmp=arrFrom[i]; if(source.equals(tmp)){ return arrTo[i]; } } // return SystemHWUtil.EMPTY; throw new RuntimeException("can not find"); }
应用:
@Test public void test_sedY(){ String base[]=new String[]{"a","bc","c","d"}; String result=RegexUtil.sedY("a", base, new String[]{"1a","2b","3c","4d"}); System.out.println(result); }
6,获取请求头中的 cookie
/*** * regex 中必须包含小括号 * @param source * @param regex * @return */ public static String sed(String source, String regex) { Pattern p = Pattern.compile(regex, Pattern.MULTILINE); Matcher m = p.matcher(source); if (m.find()) { return m.group(1); } return null; } String cookieLine = RegexUtil.sed(requestHeaderAndServletPath, "(Cookie:[^:]+)");
7,篡改请求头中的 Host
/*** * 篡改请求头中的Host * @param string * @param domain * @return */ public static String replaceHost(String string, String domain) { string = string.replaceAll("(Host:[\\s]*)[a-zA-Z\\.-]+", "$1" + domain);//blog.yhskyc.com string = string.replaceAll("(Host:[\\s]*)[\\d\\.]+:[\\d]+", "$1" + domain);//127.0.0.1:8080 return string; }
注意:[.]+ 不能匹配任意字符,应该是.+
相关推荐
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB数据库...
本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性...
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB数据库...
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB...
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB...
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB数据库...
1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5. MaxDB数据库...