正则表达式最基本应用

 正则表达式概述:

正则表达式也是一门语言,作用主要用于文本校验查找。

 正则表达式使用:

String类型中有一个方法matches()在文档上可以查找对应的文档。

 测试一:

@Test
    public void test03(){
        String s = "12";
        //匹配0-9
//        boolean matches = s.matches("[\\d]");
        //匹配0-9
//        boolean matches = s.matches("[0-9]");
        //匹配非数字
        boolean matches = s.matches("[\\D]");
        System.out.println("匹配非数字 = " + matches);
        //匹配空白
        boolean matches1 = s.matches("[\\s]");
        System.out.println("匹配空白 = " + matches1);
        //匹配非空白  %^&*(
        String s1 = "a";
        boolean matches2 = s1.matches("[\\S]");
        System.out.println("匹配非空白 = " + matches2);

        //匹配单词:[a-zA-Z_0-9] 包括_
        boolean matches3 = s1.matches("[\\w]");
        System.out.println("匹配单词 = " + matches);
        //匹配非单词
        String s2 = "@";
//        String s2 = "_";
        boolean matches4 = s2.matches("[\\W]");
        System.out.println("匹配非单词 " + matches4);

    }

测试二:

+ : 出现一次或多次
*:  出现0次或多次
?:  出现0次或1次

\d{5}恰好出现5次
\d{3,}至少出现3次
\d{1,8} 可能出现一次到八次

[^ABC]  在[]里面^表示取反
        ^用在[]外边 或者正常正则表达式前 表示以什么开头

        $ 表示以什么结尾

@Test
    public void test05(){
        // [^ABC]
        String s = "A";
        boolean matches = s.matches("[^ABC]");
        System.out.println("matches = " + matches);


    }
    @Test
    public void test04(){
        /*
        + : 出现一次或多次
        *:  出现0次或多次
        ?:  出现0次或1次

        \d{5}恰好出现5次
        \d{3,}至少出现3次
        \d{1,8} 可能出现一次到八次

        [^ABC]  在[]里面^表示取反
                ^用在[]外边 或者正常正则表达式前 表示以什么开头

                $ 表示以什么结尾

            */
        String s = "61";
//        boolean matches = s.matches("[\\w]+");
        boolean matches = s.matches("[\\w]");
        //+ : 出现一次或多次
        System.out.println("matches = " + matches);

        String m = "1234";
//        String m = "12345";  \d{5}恰好出现5次
        boolean matches1 = m.matches("\\d{5}");
        System.out.println("matches1 = " + matches1);

        String phone = "12943071961";
        //第一位是1第二位是【35789中的一位】后面9位是数字 但是写法不严谨
//        boolean matches2 = phone.matches("1[35789]\\d{9}]");

        boolean matches2 = phone.matches("^1[35789]\\d{9}$");
        System.out.println("matches2 = " + matches2);


    }
    @Test
    public void test03(){
        String s = "12";
        //匹配0-9
//        boolean matches = s.matches("[\\d]");
        //匹配0-9
//        boolean matches = s.matches("[0-9]");
        //匹配非数字
        boolean matches = s.matches("[\\D]");
        System.out.println("匹配非数字 = " + matches);
        //匹配空白
        boolean matches1 = s.matches("[\\s]");
        System.out.println("匹配空白 = " + matches1);
        //匹配非空白  %^&*(
        String s1 = "a";
        boolean matches2 = s1.matches("[\\S]");
        System.out.println("匹配非空白 = " + matches2);

        //匹配单词:[a-zA-Z_0-9] 包括_
        boolean matches3 = s1.matches("[\\w]");
        System.out.println("匹配单词 = " + matches);
        //匹配非单词
        String s2 = "@";
//        String s2 = "_";
        boolean matches4 = s2.matches("[\\W]");
        System.out.println("匹配非单词 " + matches4);

    }
    @Test
    public void test02(){
        String s = "-";
        boolean matches = s.matches("\\D");
        System.out.println("matches = " + matches);
    }
    @Test
    public void test01(){
        String s = "1";
//        boolean a = s.matches("A");
        //[]可以匹配ABC
        boolean a = s.matches("[ABC]");
        //System.out.println("a = " + a);

        //匹配 0-9
        boolean matches = s.matches("[0-9]");
        System.out.println("matches = " + matches);

        //匹配[A-Z]匹配大写A到Z
        //匹配[A-Za-z]匹配大写和小写字母
        //匹配[A-Za-z0-9]匹配既有字母又有数字

        //


    }

 测试三:

@Test
    public void test6(){
        String str = "1Hello233World334java4423atguigu5";
       /* //Arrays.toString(split) = [, Hello, World, java, atguigu]
        //以数字分割  会造成结果首位是,
       // String[] split = str.split("[\\d]");
        //System.out.println("Arrays.toString(split) = " + Arrays.toString(split));
        */
        //将开头的数字和结尾的数字去掉
        String s = str.replaceAll("^\\d|\\d$", "");
        //一位数字切割
//        String[] split = s.split("[0-9]");
//        System.out.println("Arrays.toString(split) = " + Arrays.toString(split));


        //+ 匹配一位或多位   * 匹配0位或多为   ?  匹配0位或1位
        String[] split = s.split("\\d+");
        System.out.println("Arrays.toString(split) = " + Arrays.toString(split));

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值