加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式工具类,验证数据是否符合规范

发布时间:2020-12-14 05:45:58 所属栏目:百科 来源:网络整理
导读:package com.JUtils.base; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达式工具类,验证数据是否符合规范 * */ public class RegexUtils { /** * 判断字符串是否符合正则表达式 * * @param str * @param regex * @return
package com.JUtils.base;

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

/**
 * 正则表达式工具类,验证数据是否符合规范
 *
 */
public class RegexUtils {
    
    /**
     * 判断字符串是否符合正则表达式
     *
     * @param str
     * @param regex
     * @return
     */
    public static boolean find(String str,String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        boolean b = m.find();
        return b;
    }
    
    /**
     * 判断输入的字符串是否符合Email格式.
     *
     * @param email
     *                 传入的字符串
     * @return 符合Email格式返回true,否则返回false
     */
    public static boolean isEmail(String email) {
        if (email == null || email.length() < 1 || email.length() > 256) {
            return false;
        }
        Pattern pattern = Pattern.compile("^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$");
        return pattern.matcher(email).matches();
    }
    
    /**
     * 判断输入的字符串是否为纯汉字
     *
     * @param value
     *                 传入的字符串
     * @return
     */
    public static boolean isChinese(String value) {
        Pattern pattern = Pattern.compile("[u0391-uFFE5]+$");
        return pattern.matcher(value).matches();
    }
    
    /**
     * 判断是否为浮点数,包括double和float
     *
     * @param value
     *             传入的字符串
     * @return
     */
    public static boolean isDouble(String value) {
        Pattern pattern = Pattern.compile("^[-+]?d+.d+$");
        return pattern.matcher(value).matches();
    }
    
    /**
     * 判断是否为整数
     *
     * @param value
     *             传入的字符串
     * @return
     */
    public static boolean isInteger(String value) {
        Pattern pattern = Pattern.compile("^[-+]?[d]+$");
        return pattern.matcher(value).matches();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读