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

 

正则表达示是一个很神奇的东西,在有些地方用得好的话,可以省去好多的脑力活动。深入的研究一下正则表达示很有必要。

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