Loading

JAVA正则提取字符串中的日期

在我们解析字符串的时候,有这么一个需求,需要提取字符中的日期,例如:"开奖日期:2021年3月28日 兑奖截止日期:2021年5月26日" 

输入样例:

开奖日期:2021年3月28日 兑奖截止日期:2021年5月26日

输出样例:

2021年3月28日
2021年5月26日

代码实现

  public void fun() {
        String text = "开奖日期:2021年3月28日 兑奖截止日期:2021年5月26日";
        Pattern p = Pattern.compile("(\\d{4})年(\\d{1,2})月(\\d{1,2})日");
        Matcher m = p.matcher(text);
        while (m.find()) {
            System.out.println(m.group(0));
        }
    }

posted @ 2021-04-01 23:28  Roc-xb  阅读(89)  评论(0)    收藏  举报

易微帮源码


易微帮官网