Regex 正则表达式的练习

package com.company.danei;

import java.util.Scanner;

/**
 * @Author: 
 * @Date: 2021-05-09
 * @Description: 通过正则表达式校验身份证号码是否正确
 * 1、接收用户输入的一行字符串
 * 2、定义正则表达式
 * 3、通过matches方法比较输入的字符串是否符合正则表达式
 **/
public class Test_Regex {
    public static void main(String[] args) {
        //定义正则表达式
        String regex = "[0-9]{17}[0-9X]";
        boolean b=true;
        do {
            //接收用户输入的一行字符串
            System.out.println("please input the number");
            String str = new Scanner(System.in).nextLine();
            //通过matches方法比较输入的字符串是否符合正则表达式
           if(str.matches(regex)) {
               System.out.println("ok");
               break;
           };
        }while (b);
    }
}
原文地址:https://www.cnblogs.com/xiaohai2003ly/p/14749262.html