Java Regex练习(1) (2024.7.21)

        Regex1

package RegexExercise20240721;
import java.util.Scanner;
public class RegexExercise {
    public static void main(String[] args) {
        // 正则表达式
        /* 假如现在要求校验一个qq号码是否正确。
            规则:6位及20位之内,0不能在开头,必须全部是数字。
            先使用目前所学知识完成校验需求然后体验一下正则表达式检验。
        */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个qq号");
        String qq = sc.nextLine();
        if (qq.matches("[1-9]\\d{5,19}")) {  // [1-9]表示第一个数字只能是1-9
            System.out.println("此qq号合法");      //  \\d表示是数组的预定义字符,{5,19}
        } else {                                  //  表示除了第一位,还有5-19位数字
            System.out.println("此qq号非法");
        }

    }
}

        Regex2

package RegexExercise20240721;
import java.util.Scanner;
public class RegexExercise2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //1.要求字符串是小写辅音字符开头,后面纯字母,共六位
        /*System.out.println("请输入一个字符串");
        String testString = sc.nextLine();
        if (testString.matches("[a-z&&[^aeiou]][a-zA-Z]{5}")) {
            System.out.println("合法字符串");
        } else {
            System.out.println("非法字符串");
        }*/

        //2.要求字符串是aeiou中的某个字符开头,后跟ad
        System.out.println("请输入一个字符串");
        String testString = sc.nextLine();
        if (testString.matches("[aeiou]ad")) {
            System.out.println("合法字符串");
        } else {
            System.out.println("非法字符串");
        }
    }
}

        Regex3

package RegexExercise20240721;
import java.util.Scanner;
public class RegexExercise3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //练习:以字符串的形式打印一个双引号
        //在Java中\为转义字符
//        System.out.println("\"\""); // 只打""Java会认为你打了个空的字符串,所以说要用\"进行转义
//        System.out.println("\\"); // 同样,\会认为是一个转义字符,所以说要用\\来达到输出\的效果
        // 预定义字符
        // .表示任意一个字符
//        System.out.println("请输入一个字符串");
//        String str1 = sc.nextLine();
//        System.out.println(str1.matches(".{6}"));

        // /d表示[0-9]
        System.out.println("123456".matches("\\d{6}"));
        System.out.println("126".matches("\\d{6}")); // 位数不够
        System.out.println("1234aa".matches("\\d{6}")); // 不是纯数字
        System.out.println("-------------------------------------------");

        // /D表示[^0-9]
        System.out.println("123456".matches("\\D{6}")); // 不能出现数字
        System.out.println("abcdef".matches("\\D{6}"));
        System.out.println("abc132".matches("\\D{6}")); // 不能出现数字
        System.out.println("-------------------------------------------");

        // \s表示空白字符[\t\n\x0B\f\r]
        System.out.println(" \t".matches("\\s{2}")); // 大抵用得比较少
        System.out.println("-------------------------------------------");

        // \S表示非空白字符[^\s]
        System.out.println("123456".matches("\\S{6}")); // 无空白字符
        System.out.println("123 56".matches("\\S{6}")); // 有一个空白字符
        System.out.println("阿斯顿阿斯顿".matches("\\S{6}"));
        System.out.println("-------------------------------------------");

        // \w表示单词字符[a-zA-Z_0-9]
        System.out.println("123abc__".matches("\\w{8}"));
        System.out.println("123abc_$".matches("\\w{8}")); // 出现了非单词字符
        System.out.println("123abc_啊".matches("\\w{8}"));
        System.out.println("-------------------------------------------");

        // \W表示非单词字符[^\w]
        System.out.println("啊水水阿斯顿".matches("\\W{6}"));
        System.out.println("123阿斯顿".matches("\\W{6}")); // 出现了单词字符


    }
}

        Regex4

package RegexExercise20240721;
import java.util.Scanner;
public class RegexExercise4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 正则表达式的数量词
        // 必须是数字 字母 下划线 至少6位
        System.out.println("123456789".matches("\\w{6,}")); // {6,}表示至少六位
        System.out.println("123456789".matches("\\w{6}"));  // {6} 表示刚好六位
        System.out.println("-----------------------------------------");
        // 因为{,}是一个范围,所以说{6,}的范围就是6到正无穷,所以说是至少六位

        // 必须是数字和字符 必须是4位
        System.out.println("1234".matches("[a-zA-Z_0-9]{4}"));
        System.out.println("abcd".matches("[\\w&&[^_]]{4}"));
        System.out.println("abd".matches("[\\w&&[^_]]{4}")); // 位数不够
        System.out.println("a_2d".matches("\\w{4}"));
        System.out.println("-----------------------------------------");

        // 出现0次或1次----> x?
        System.out.println("123456a".matches("\\d{6}\\w?")); // \w出现了一次
        System.out.println("123456".matches("\\d{6}\\w?"));  // \w没有出现
        System.out.println("123456ab".matches("\\d{6}\\w?"));// \w出现了两次->false
        System.out.println("-----------------------------------------");

        // 出现0次到多次----> x*
        System.out.println("123456abc".matches("\\d{6}\\w*")); // \w出现了多次
        System.out.println("123456".matches("\\d{6}\\w*"));    // \w出现了0次
        System.out.println("123456a".matches("\\d{6}\\w*"));   // \w出现了1次
        System.out.println("-----------------------------------------");

        // 出现1次到多次----> x+
        System.out.println("123456abc".matches("\\d{6}\\w+"));
        System.out.println("123456".matches("\\d{6}\\w+")); // \w没有出现->false
        System.out.println("123456a".matches("\\d{6}\\w+"));
        System.out.println("-----------------------------------------");
    }
}

 

 

 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值