正则表达式

正则表达式

Test01

package _11正则表达式_演示;

/*
普通方式校验字符串:
    判断用户输入的身份证号码是否合法。
    规则为:号码为18位,不能以数字0开头,前17位只可以是数字,最后一位可以是数字或者大写字母X。
 */

public class RegexTest1 {

    public static boolean checkId(String id){
        //号码为18位.如果不是18位,返回false
        if(id.length()!=18){
            return false;
        }
        //不能以数字0开头,如果以数字0开头,返回false
        if(id.startsWith("0")){
            return false;
        }
        //前17位只可以是数字,如果前17位中有不是数字的,返回false
        for(int i=0; i<17; i++){
            char ch = id.charAt(i);
            if(ch>'9' || ch<'0'){
                return false;
            }
        }
        //最后一位可以是数字或者大写字母X
        char last = id.charAt(17);
        if(last=='X' || (last>='0' && last<='9')){
            return true;
        }else {
            return false;
        }

    }

    //测试
    public static void main(String[] args) {
        System.out.println( checkId("445221199909091234") );
        System.out.println( checkId("44522119990909123X") );
        System.out.println( checkId("44522119990909123") );
        System.out.println( checkId("4452211999090912345") );
        System.out.println( checkId("045221199909091234") );
        System.out.println( checkId("4452A1199909091234") );
        System.out.println( checkId("44522119990909123x") );
    }



}

Test02

package _11正则表达式_演示;

/*
正则表达式校验字符串:
    判断用户输入的身份证号码是否合法。
    规则为:号码为18位,不能以数字0开头,前17位只可以是数字,最后一位可以是数字或者大写字母X。
 */
public class RegexTest2 {

    public static boolean checkId(String id){
        if(id==null){
            return false;
        }else {
            return id.matches("[1-9]\\d{16}[0-9X]");
        }
    }

    //测试
    public static void main(String[] args) {
        System.out.println( checkId("445221199909091234") ); //true
        System.out.println( checkId("44522119990909123X") );//true
        System.out.println( checkId("44522119990909123") );
        System.out.println( checkId("4452211999090912345") );
        System.out.println( checkId("045221199909091234") );
        System.out.println( checkId("4452A1199909091234") );
        System.out.println( checkId("44522119990909123x") );
    }

}

表达式语法

Test01

package _12正则表达式语法_了解;

/*
    1.范围匹配 []
        [abc]:匹配abc中任意一个字符。
        [a-z]:匹配小写字母a-z中的一个。
        [A-Z]:匹配大写字母A-Z中的一个。
        [0-9]:匹配数字0-9中的一个。

        组合:
        [a-zA-Z0-9]:匹配a-z或者A-Z或者0-9之间的任意一个字符。
        [a-dm-p]: 匹配a-d或m-p之间的任意一个字符。

        排除:
        [^abc]:匹配除a、b、c之外的任意一个字符。
        [^a-z]:匹配除小写字母外的任意一个字符。
 */
public class Demo1 {

    public static void main(String[] args) {

        //[abc] 匹配abc中任意一个字符。
        System.out.println( "a".matches("[abc]") );//true
        System.out.println( "ab".matches("[abc]") ); //false
        System.out.println( "b".matches("[abc]") ); //true
        System.out.println( "c".matches("[abc]") ); //true

        //[abc][abc] 匹配两个字符,第一个字符可以是abc中的一个,第二个字符可以是abc中的一个
        System.out.println( "cb".matches("[abc][abc]") );

        //匹配任意一个小写字母
        System.out.println( "c".matches("[a-z]") );

        //匹配任意一个字母
        System.out.println( "c".matches("[a-zA-Z]") );
        System.out.println( "C".matches("[a-zA-Z]") );

        //匹配任意一个字母或者空格
        System.out.println( " ".matches("[a-z A-Z]") );
    }
}

Test02

package _12正则表达式语法_了解;

/*
    2.预定义字符
        "." : 匹配一个任意字符

        "\d": 匹配一个数字字符,相当于[0-9]
        "\D": 匹配一个非数字,相当于[^0-9]

        "\s": 匹配一个空白字符
        "\S": 匹配一个非空白字符

        "\w": 匹配一个单词字符,包括大小写字母,数字,下划线,相当于[a-zA-Z0-9_]
        "\W": 匹配一个非单词字符
 */
public class Demo2 {
    public static void main(String[] args) {

        //匹配一个数字字符
        System.out.println("5".matches("\\d"));

        //匹配一个空白字符
        System.out.println(" ".matches("\\s"));

        //匹配一个单词字符,包括大小写字母,数字,下划线,相当于[a-zA-Z0-9_]
        System.out.println("A".matches("\\w"));
        System.out.println("a".matches("\\w"));
        System.out.println("6".matches("\\w"));
        System.out.println("_".matches("\\w"));
        System.out.println(" ".matches("\\w"));

        //"." : 匹配一个任意字符
        System.out.println(" ".matches("."));
        System.out.println("a".matches("."));
        System.out.println("中".matches("."));
        System.out.println("1".matches("."));

        //匹配任意的长度为3的字符串
        System.out.println("1fr".matches("..."));
        System.out.println("   ".matches("..."));

        //匹配 . 字符串
        System.out.println("a".matches("\\."));
        System.out.println(".".matches("\\."));
    }
}

Test03

package _12正则表达式语法_了解;

/*
    3.数量词(限定符)
        ?   0次或1次
        *   0次或多次 (任意次)
        +   1次或多次
        {n}    重复n次
        {n,}   重复n次以上 ( >= n次)
        {n,m}  重复n到m次(包括n和m)
 */
public class Demo3 {
    public static void main(String[] args) {
        //匹配长度为4的数字 \\d{4}


        //匹配密码 5-8位的密码 (密码只能是数字,字母,下划线) \\w{5,8}
        System.out.println( "ithe_i".matches("\\w{5,8}") );

        //衣服尺码 L XL  XXL  XXXL  (X可以是0到多个)
        System.out.println( "L".matches("X*L") );
        System.out.println( "XXXL".matches("X*L") );

        //匹配任意一个小写英文单词 a  at  for love
        System.out.println( "a".matches("[a-z]+") );
        System.out.println( "love".matches("[a-z]+") );

        //匹配长度为5的数字验证码
        //System.out.println( "34546".matches("[0-9][0-9][0-9][0-9][0-9]") );
        //System.out.println( "43575".matches("\\d\\d\\d\\d\\d") );

        System.out.println( "43575".matches("\\d{5}") );
        System.out.println( "34546".matches("[0-9]{5}") );

        //匹配长度为6的字符串,然后第一个字符是数字或者字母或者下划线,后面的5个字符串都是数字
        System.out.println( "".matches("\\w\\d{5}") );

        System.out.println( "1".matches("[0-9]?") ); //内容可以没有,也可以是一个数字
        System.out.println( "".matches("[0-9]?") ); //内容可以没有,也可以是一个数字

        System.out.println( "123".matches("[0-9]*") ); //内容可以没有,也可以是任意个数字
        System.out.println( "".matches("[0-9]*") ); //内容可以没有,也可以是任意个数字

        System.out.println( "3456".matches("[0-9]+") ); //匹配一到多个数字

    }

}

Test04

package _12正则表达式语法_了解;

/*
    4.括号分组 ()
        正则表达式中用小括号()来做分组,也就是括号中的内容作为一个整体。
 */
public class Demo4 {
    public static void main(String[] args) {
        //匹配 over! 连续出现3次以上
       System.out.println( "over!over!over!".matches("(over!){3,}") );


    }
}

测试1

package _12正则表达式语法_了解;

public class Test {
    public static void main(String[] args) {
        System.out.println("123".matches("[123]")); //false //[123]匹配123中的一个
        System.out.println("123".matches("[123]+")); //true  //[123]+

        System.out.println("111".matches("[123]+")); //true  //[123]+ 匹配123任意去组合

        System.out.println("123".matches("[123]{3}")); //true 123任意去组合,组合长度只能是3

        System.out.println("123".matches("[123]*")); //true
        System.out.println("".matches("[123]*")); //true 匹配任意长度,但是内容只能从123里面去组合

        System.out.println("123".matches("[123].")); //false 匹配长度固定是2,第一个是123中的一个,第二个任意
        System.out.println("1a".matches("[123].")); //false 匹配长度固定是2,第一个是123中的一个,第二个任意
    }
}

测试2

package _12正则表达式语法_了解;

public class Test2 {
    public static void main(String[] args) {

        String date = "2022-12-12";

        //用/替换日期的-
        String s = date.replaceAll("-", "/");
        System.out.println(s);

        //把每个单词的空格统一成一个
        String word = "i   love  java     hello   world";
        String s1 = word.replaceAll(" +", " ");
        System.out.println(s1);

        //-------------------------------------
        String d = "2022-12-12";
        String[] arr = d.split("-");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println("-------------");

        //解析出每个数字,累加
        String numbers = "11  22    33 44    55";
        String[] array = numbers.split(" +");

        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            int a = Integer.parseInt(array[i]);
            sum += a;
        }
        System.out.println(sum);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值