几个可以用到的正则表达式

package lbi.flow.util;

import net.sf.json.regexp.RegexpMatcher;

public class RegexpUtils {
    /**
     * 匹配7c:d1:c3:73:d6:72形式mac
     * 
     * @return
     */
    public static RegexpMatcher getMacMatcher() {
        return net.sf.json.regexp.RegexpUtils
                .getMatcher("[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]");
    }

    /**
     * 匹配20140430235959形式日期
     * 
     * @return
     */
    public static RegexpMatcher getTimeMatcher() {
        return net.sf.json.regexp.RegexpUtils
                .getMatcher("^(?:(?!0000)[0-9]{4}(?:(?:0[1-9]|1[0-2])(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]$");
    }

    /**
     * 匹配2014-04-30 23:59:59形式日期
     * 
     * @return
     */
    public static RegexpMatcher getTimeMatcher2() {
        return net.sf.json.regexp.RegexpUtils
                .getMatcher("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");
    }

    /**
     * 匹配20140430形式日期
     * 
     * @return
     */
    public static RegexpMatcher getDateMatcher() {
        return net.sf.json.regexp.RegexpUtils
                .getMatcher("^(?:(?!0000)[0-9]{4}(?:(?:0[1-9]|1[0-2])(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
    }

    /**
     * 匹配2014-04-30形式日期
     * 
     * @return
     */
    public static RegexpMatcher getDateMatcher2() {
        return net.sf.json.regexp.RegexpUtils
                .getMatcher("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
    }
}

工作时候正好用到了,顺便记录下来,以备后用。

参考地址:http://blog.csdn.net/lxcnn/article/details/4362500

    http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html

posted @ 2014-05-20 13:51  黑面馒头  阅读(326)  评论(0编辑  收藏  举报