正则表达式的获得

来源:互联网 发布:js点击链接弹出下载框 编辑:程序博客网 时间:2024/05/17 03:26

<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);     }}