栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java案例代码16-java正则表达式的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java案例代码16-java正则表达式的使用

具体看注释:

具体使用的是字符串中的函数“matches”

matches 

package Templab;

import java.util.Scanner;

public class Regeslab1 {


    public static void main(String[] args) {
//        checkPhone();   //手机格式,正则的监测

//        checkEmain();  //邮箱格式检测
        checkTel();  //手机号的监测

    }


    public static void checkTel(){
        Scanner sc=new Scanner(System.in);

        while (true){
            System.out.println("请输入您的电话号码:");
            String tel=sc.next();

            //判断电话号码的格式是否正确
            //010-12345678   01012345678
            if(tel.matches("0\d{2,6}-?\d{5,20}")){
                System.out.println("格式正确,注册完成!");
                break;


            }else{
                System.out.println("格式有误!");
            }
        }

    }



    public static void checkEmain(){
        Scanner sc=new Scanner(System.in);

        while (true){
            System.out.println("请输入您的注册邮箱:");
            String email=sc.next();

            //判断邮箱的格式是否正确
            // @符号前面1~~30个字符,数字等,@符号后2~~20位字符,后面是“.2~~20个字符",一到2组,类似
            //dsddsds@yahoo.com.cn类似这样的邮箱。
            String emainreg="\w{1,30}@[a-zA-Z0-9]{2,20}(\.[A-Za-z0-9]{2,20}){1,2}";
            if(email.matches(emainreg)){
                System.out.println("邮箱格式正确,注册完成!");
                break;


            }else{
                System.out.println("格式有误!");
            }
        }

    }


    //手机方法检测
    public static void checkPhone(){
        Scanner sc=new Scanner(System.in);

        while (true){
            System.out.println("请输入您的注册收集号码:");
            String phone=sc.next();

            //判断手机号码的格式是否正确
            if(phone.matches("1[3-9]\d{9}")){
                System.out.println("手机号码格式正确,注册完成!");
                break;


            }else{
                System.out.println("格式有误!");
            }
        }

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号