正则表达式获取日期

来源:互联网 发布:java计算时间间隔 编辑:程序博客网 时间:2024/05/18 01:08
 

//获取日期
public void testRegex() {
        Pattern pattern = Pattern.compile("20\\d{2}(-|\\/)((0[1-9])|(1[0-2]))(-|\\/)((0[1-9])|([1-2][0-9])|(3[0-1]))");
        String str = "中央政府门户网站 www.gov.cn   2008年01月22日   来源:新华社";
        str = str.replace("年", "-").replace("月", "-").replace("日", "-");
        Matcher matcher = pattern.matcher(str);
        if(matcher.find()){
            MatchResult result = matcher.toMatchResult();
            System.out.println(result.group());
        }
        assertTrue(true);
    }

原创粉丝点击