正则表达式的获得

<img src="test.jpg" width="60px" height="80px"/>
如果用正则匹配src中内容
非懒惰模式匹配
src=”.*”
匹配结果是:src=”test.jpg” width=”60px” height=”80px”
意思是从=”往后匹配,直到最后一个”匹配结束
懒惰模式正则:
src=”.*?”
结果:src=”test.jpg”
因为匹配到第一个”就结束了一次匹配。不会继续向后匹配。因为他懒惰嘛。

公司框架中的正则表达式的工具类:

public class PatternUtils {

    /**
     * 正则表达式:验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";

    /**
     * 正则表达式:验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";

    /**
     * 正则表达式:验证手机号,^1[34578]\\d{9}$或^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
     */
    public static final String REGEX_MOBILE = "^1[34578]\\d{9}$";

    /**
     * 正则表达式:验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";

    /**
     * 正则表达式:验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

    /**
     * 正则表达式:验证URL 
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式:验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";


    /**
     * 非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
     * positive 
     */
    public static final String  REGEX_NON_ZERO_POSITIVE_INTEGERS = "^\\+?[1-9][0-9]*$";

    /**
     * 非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
     */
    public static final String REGEX_NON_ZERO_NEGATIVE_INTEGERS = "^-[1-9]\\d*$";

    /**
     * 非负整数:^\d+$ 或 ^[1-9]\d*|0$
     */
    public static final String  REGEX_ZERO_POSITIVE_INTEGERS = "^\\d+$";

    /**
     * 非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
     */
    public static final String  REGEX_ZERO_NEGATIVE_INTEGERS = "^-[1-9]\\d*|0$";

    /**
     * 非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
     */
    public static final String  REGEX_NONNEGATIVE_FLOATING = "^\\d+(\\.\\d+)?$";

    /**
     *  非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
     */
    public static final String  REGEX_NONPOSITIVE_FLOATING = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";

    /**
     *  正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
     */
    public static final String  REGEX_POSITIVE_FLOATING = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";

    /**
     *  负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
     */
    public static final String  REGEX_NEGATIVE_FLOATING = "^-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*)$";

    /**
     *  浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
     */
    public static final String  REGEX_FLOATING = "^(-?\\d+)(\\.\\d+)?$";

    /**
     * 验证整数以及小数的
     */
    public static final String  REGEX_INT_NUMBER_DECIMALS = "([1-9]+[0-9]*|0)(\\.[\\d]+)?";


    // 判断数据格式是否一致
    private static boolean isMatch(String number,String regex) throws Exception{
        if(StringUtils.isBlank(number)){
            throw new Exception("输入的数字为空");
        }
        return Pattern.compile(regex).matcher(number).matches();
    }

    /**
     * 验证非负浮点数
     */
    public static boolean isNonNegativeFloating(String number) throws Exception{
        return isMatch(number,REGEX_NONNEGATIVE_FLOATING); 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值