java正則表達式

正則表達示

  瞭解正則表達示,首先要明白爲什麼需要正則表達示。

正則表達示是一門自然語言,並不單純的依賴於某一種語言而存在,

在程序開發的過程當中,我們經常需要處理一些字符串,比如匹配呀,查找,替換,判斷字符串的情況,如果單純的僅僅用編碼的方法一個一個來拼接,或者其他的方式來處理,我們需要浪費的太多的時間和精力,有了它,使得我們字符串處理簡單而高效。

首先我們來了解一下在正則表達式中需要用的替換字符

1、  .(點號,匹配所有的字符) [](匹配指定的字符這個是單個字符的匹配)  括號”()“是[]的擴展,可以匹配多個,用|符號隔開  |  (或)  {}(來說明字符出現的次數)  ^(表示相反的字符)

2、說明字符出現次數的符號有

   *(0次或多次) +(1次或多次) ?(0次或一切) {m} 指定的M次 {n,m} 指定的M-N次

3、快捷符號:

 

 

4、敏感詞彙的替換(replaceAll()等字體串中的方法就自己去體會了)

 a.交集和並集的例子


public class regex3 {

    /** @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 字符串的交集 並集
        String[] str = { "A1", "B3", "E4", "F3", "G5", "M9" };
        String reg = "[A-F&&E-M][0-9]";// 交集
        for (String string : str) {
            if (string.matches(reg)) {
                System.out.println(string);
            }
        }
        System.out.println("^^^^^^^^^^^^^^^^^^");
        String reg1 = "[A-B[G-M]][0-9]";// 並集
        for (String string : str) {
            if (string.matches(reg1)) {
                System.out.println(string);
            }
        }
       
       
        String str2 = "*";
        String reg2="\\D";
        if(str2.matches(reg2)){
            System.out.println(str2);
        }
    }

}

b.通過Pattern來匹配的郵箱例子:

public class Regex2 {

    /**  @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String mystr = "The theatre is the greatest from of live"+
                "entertainment according to thespians.";
        String reg1 = "the[a-z]";
        //截取包含正則表達式的字符串
        Pattern pattern = Pattern.compile(reg1);
        //首先把正則表達示編譯成一個pattern對象
        Matcher matcher = pattern.matcher(mystr);
        //根據樣式對象pattern針對這字符串生成一個掃描器matcher
        while(matcher.find()){
            System.out.println(matcher.group());//用group用來得到匹配的字符
        }
       
        String str1="[email protected]";
        parse(str1);
       
       
    }
    private static void parse(String line) {
        Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
        Matcher m = p.matcher(line);
        while(m.find()) {
            System.out.println(m.group());
        }
    }


}

c.敏感詞彙的替換


public class Regex5 {

    /** @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new File("a.txt");
        try {
            BufferedReader fin = new BufferedReader(new FileReader(file));
            String str = "";
            String regex = "國民黨";
            while ((str = fin.readLine()) != null) {
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(str);
                StringBuffer sb = new StringBuffer();
                while (m.find()) {
                    m.appendReplacement(sb, "vickyi");//"vickyi"用來替換"國民黨" 把找到的替換放到SB中
                }
                m.appendTail(sb);
                System.out.println(sb.toString());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
 

 

正則表達示是一個很神奇的東西,在有些地方用得好的話,可以省去好多的腦力活動。深入的研究一下正則表達示很有必要。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章