public class RegexDemo {

    public static void main(String[] args) {
        
        /*
         * 正则表达式:
         * 专门用于对字符串的操作
         * 规则是由符号组成的,用于操作字符串变得简单
         * 弊端:阅读性降低了。
         * 所以学习正则其实就是学习符号的使用。
         * 
         * 1.匹配
         * String 类中就提供了匹配boolean matches(regex)的方法。
         * 
         * 2.切割
         * 
         * 3.替换
         * 
         * 4.获取
         * 
         * 
         */
        
//        校验QQ号5-15位,不能0开头,必须是数字
//        String QQ = "a234";
//        boolean b = QQ.matches("[1-9]\\d{4,14}");
//        System.out.println(QQ+":"+b);

        String temp = "a123456";
        boolean b1 = temp.matches("a\\d?");//?表示一次或一次也没有(最多一次)
        boolean b2 = temp.matches("a\\d+");//+表示一次或多次(至少一次)
        boolean b3 = temp.matches("a\\d*");//*表示0次或多次(有没有都行)
        boolean b4 = temp.matches("a\\d{5}");//{5}紧挨着的规则必须是五次(五个数字)
        boolean b5 = temp.matches("a\\d{5,}");//至少五次
        boolean b6 = temp.matches("a\\d{5,7}");//五到七次
        
        System.out.println(b5);
        
    }

}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest1 {

    public static void main(String[] args) {
        
//        matchesDemo(); //匹配
        
//        splitDemo();  //切割
        
//        replaceDemo(); //替换
        
//        getDemo(); //查找
        
    }

    public static void getDemo() {
        
        /*
         * 实现获取,将符合规则的内容取出来
         * 用到正则表达式对象 java.util.regex.Pattern
         * 
         * 1.将字符串规则封装成Pattern对象,compile(regex);
         * 2.通过正则对象获取匹配器对象。用对将正则规则作用到要操作的字符串上
         * 3.通过匹配器对象的方法对字符串进行操作
         * 
         * Pattern p = Pattern.compile("a*b");//将规则编译成对象
         * Matcher m = p.matcher("aaaaab");//和要操作的字符串进行关联,生成匹配器对象
         * boolean b = m.matches();//使用匹配对象方法对字符串进行操作
         * 
         */
        //获取字符串中符合规则的内容
        String temp = "da jia zhu yi la,ming tian fang jia la!";
        //取出由三个字母组成的单词
        String regex = "\\b[a-zA-Z]{3}\\b";
        
        //1.将规则编译成Pattern对象
        Pattern p = Pattern.compile(regex);
        
        //2.和字符串关联,获取匹配器对象。
        Matcher m  = p.matcher(temp);
        
//        System.out.println(m.find());
//        System.out.println(m.group());
        
        while(m.find()){//查找
            System.out.println(m.start()+":"+m.group()+":"+m.end());
        }
        
        
        
    }

    public static void replaceDemo() {
        
//        String temp = "15856535987";
//        String regex = "(\\d{3})\\d{4}(\\d{4})";
//        temp = temp.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        
        String temp = "qwer###et&&&oizzzzupo";
        temp = temp.replaceAll("(.)\\1+", "$1");
        
        //$可以再多参数时,后面的参数可以通过$编号的形式来取到前一个参数的组
        //15800001111   158****1111
        
        System.out.println(temp);
    }

    public static void splitDemo() {
        
//        String temp = "zhangsan   lisi       wangwu";
//        String regex = " +";
        
//        String temp = "zhangsan.lisi.wangwu";
//        String regex = "\\.";
        
        String temp = "qwer###et&&&oizzzzupo";
        String regex = "(.)\\1+";//为了实现规则的复用,用()将需要复用的规则封装,
                                //就成为了正则表达式中的组,每一个组都由编号,从1开始。
                                //通过使用编号就可以复用对应组的规则内容。
                                //注意:编号必须用在组的后面!!
                                //也就是说,先封装完再调用。
        
        
        String[] names = temp.split(regex);
        for(String name:names){
            System.out.println("--"+name+"--");
        }
        
    }

    public static void matchesDemo() {
        
        //需求:校验手机号码是否正确
        
        String tel = "17806171162";
        String regex = "1[3578]\\d{9}";
        boolean b = tel.matches(regex);
        System.out.println(tel+":"+b);
        
    }

}

 

posted on 2016-10-03 13:07  北方丶有佳人  阅读(140)  评论(0编辑  收藏  举报