java 正则表达式

package com.te.jdkapi;

import java.util.Scanner;

/*
验证身份证的号码
 */
public class PersonDemo {
    public static void main(String[] args) {
       Scanner sc =new Scanner(System.in);
       String reg = "[1-8]\\d{16}[0-9X]";
        while (true){
            if (sc.next().matches(reg)){
                System.out.println("匹配正确");
                break;
            }else {
                System.out.println("匹配失败");
            }
        }
    }
}
package com.te.jdkapi;
/*
声明qq号的校验的正则表达式
验证qq号
 */
public class TestRegx {
    public static void main(String[] args) {
        String st = "0842616212";
        String reg = "[1-9]\\d{4,10}";
//        String s = st.replaceAll(reg, "  ");
        System.out.println(st.matches(reg));
    }
}
posted @ 2022-11-03 20:20  竹石2020  阅读(11)  评论(0编辑  收藏  举报