正則表明式(入門) 2

◆以多條件分割字符串時

1
Pattern pattern = Pattern.compile("[, |]+"); 2 String[] strs = pattern.split("Java Hello World Java,Hello,,World|Sun"); 3 for (int i=0;i<strs.length;i++) { 4 System.out.println(strs[i]); 5 }

 

◆文字替換(首次出現字符)
Pattern pattern = Pattern.compile("正則表達式");
Matcher matcher = pattern.matcher("正則表達式 Hello World,正則表達式 Hello World");
//替換第一個符合正則的數據
System.out.println(matcher.replaceFirst("Java"));

  

◆文字替換(所有)
Pattern pattern = Pattern.compile("正則表達式");
Matcher matcher = pattern.matcher("正則表達式 Hello World,正則表達式 Hello World");
//替換第一個符合正則的數據
System.out.println(matcher.replaceAll("Java"));

 

1 ◆文字替換(置換字符)
2 Pattern pattern = Pattern.compile("正則表達式");
3 Matcher matcher = pattern.matcher("正則表達式 Hello World,正則表達式 Hello World ");
4 StringBuffer sbr = new StringBuffer();
5 while (matcher.find()) {
6     matcher.appendReplacement(sbr, "Java");
7 }
8 matcher.appendTail(sbr);
9 System.out.println(sbr.toString());

 

 

 

◆驗證是否爲郵箱地址

String str="ceponline@yahoo.com.cn";
Pattern pattern = Pattern.compile("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());

 

 

 

◆去除html標記
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher("<a href=\"index.html\">主頁</a>");
String string = matcher.replaceAll("");
System.out.println(string);

 

 

 

◆查找html中對應條件字符串
Pattern pattern = Pattern.compile("href=\"(.+?)\"");
Matcher matcher = pattern.matcher("<a href=\"index.html\">主頁</a>");
if(matcher.find())
  System.out.println(matcher.group(1));
}

 

 

◆截取http://地址
//截取url
Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
Matcher matcher = pattern.matcher("dsdsds<http://dsds//gfgffdfd>fdf");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){             
    buffer.append(matcher.group());       
    buffer.append("\r\n");             
System.out.println(buffer.toString());
}

 

 

◆替換指定{}中文字

String str = "Java目前的發展史是由{0}年-{1}年";
String[][] object={new String[]{"\\{0\\}","1995"},new String[]{"\\{1\\}","2007"}};
System.out.println(replace(str,object));

public static String replace(final String sourceString,Object[] object) {
            String temp=sourceString;   
            for(int i=0;i<object.length;i++){
                      String[] result=(String[])object[i];
               Pattern    pattern = Pattern.compile(result[0]);
               Matcher matcher = pattern.matcher(temp);
               temp=matcher.replaceAll(result[1]);
            }
            return temp;
}





以正則條件查詢指定目錄下文件

 //用於緩存文件列表
        private ArrayList files = new ArrayList();
        //用於承載文件路徑
        private String _path;
        //用於承載未合併的正則公式
        private String _regexp;
       
        class MyFileFilter implements FileFilter {

             
              public boolean accept(File file) {
                try {
                  Pattern pattern = Pattern.compile(_regexp);
                  Matcher match = pattern.matcher(file.getName());               
                  return match.matches();
                } catch (Exception e) {
                  return true;
                }
              }
            }
       
       
        FilesAnalyze (String path,String regexp){
            getFileName(path,regexp);
        }
       
       
        private void getFileName(String path,String regexp) {
            //目錄
              _path=path;
              _regexp=regexp;
              File directory = new File(_path);
              File[] filesFile = directory.listFiles(new MyFileFilter());
              if (filesFile == null) return;
              for (int j = 0; j < filesFile.length; j++) {
                files.add(filesFile[j]);
              }
              return;
            }
   
       
        public void print (PrintStream out) {
            Iterator elements = files.iterator();
            while (elements.hasNext()) {
                File file=(File) elements.next();
                    out.println(file.getPath());   
            }
        }

        public static void output(String path,String regexp) {

            FilesAnalyze fileGroup1 = new FilesAnalyze(path,regexp);
            fileGroup1.print(System.out);
        }
   
        public static void main (String[] args) {
            output("C:\\","[A-z|.]*");
        }
相關文章
相關標籤/搜索