zoukankan      html  css  js  c++  java
  • java 验证邮箱格式正确性、验证字符串是否为数字

    java 验证邮箱格式正确性

     import java.util.regex.Matcher;
    import java.util.regex.Pattern;

     public class RegisterCheck {
        /**
         * 验证输入的邮箱格式是否符合
         * @param email
         * @return 是否合法
         */
    public static boolean emailFormat(String email)
        {
            boolean tag = true;
            final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            final Pattern pattern = Pattern.compile(pattern1);
            final Matcher mat = pattern.matcher(email);
            if (!mat.find()) {
                tag = false;
            }
            return tag;
        }
    }

    验证字符串是否为数字

    下面有四种方法,估计好用的应该是第三种吧。

    1.用JAVA自带的函数
    public static boolean isNumeric(String str){
    for (int i = str.length();--i>=0;){
    if (!Character.isDigit(str.charAt(i))){
    return false;
    }
    }
    return true;
    }


    2.正则表达式
    public static boolean isNumeric(String str){
    Pattern pattern = Pattern.compile("[0-9]*");
    return pattern.matcher(str).matches();
    }

    3.还是正则表达式
    public static boolean isNumeric(String str){
    if(str.matches("\\d*"){
    return true;
    }else{
    return false;
    }
    }

    4.用ascii码
    public static boolean isNumeric(String str){
    for(int i=str.length();--i>=0;){
    int chr=str.charAt(i);
    if(chr<48 || chr>57)
    return false;
    }
    return true;
    }
  • 相关阅读:
    bound 很靠近0的mle
    order statistics 为什么3维要把6块加起来
    正态分布的来历
    互斥和独立
    平稳随机过程 会不会和随机过程矛盾呢?
    请问如何理解同一个随机过程,里面的两个随机变量的期望不同呢?
    bagging和boosting以及rand-forest
    arr = map(float,arr)输出问题
    lasso数学解释
    岭回归的数学解释
  • 原文地址:https://www.cnblogs.com/pricks/p/1497205.html
Copyright © 2011-2022 走看看