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);
- }