用随机字符串内的正则表达式匹配日期 [英] Matching dates with Regex inside of a random string

查看:107
本文介绍了用随机字符串内的正则表达式匹配日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java

我收到这种字符串

"12/07/2004dddsss12/10/2010ñrrñrñr10/01/2000ksdifjsdifffffdd04/04/1998"

然后我必须在该字符串中找到一个或多个日期,日期格式: dd / mm / yyyy

Then I have to find one or more dates inside that string, date format: dd/mm/yyyy

最后我必须将匹配的日期复制到另一个字符串: 2004/12/07 12/10/2010 10/01/2000 1998年4月4日 c

Finally I have to copy to another string dates matched: "12/07/2004 12/10/2010 10/01/2000 04/04/1998"

PD:我正在使用此网站 http://regexpal.com/ 进行测试。我尝试了一些网站 regex ,有人为我工作。

PD: I'm using this website http://regexpal.com/ to test if works. I tried some website regex and anyone worked for me.

推荐答案

您可以将日期与提取的内容。

You can separate the validity of the date with the extracted content.

要提取日期:

String regex = "\\d{2}/\\d{2}/\\d{4}";

在小提琴处检查: http://fiddle.re/fa0bf

代码:

 String input = "12/07/2004dddsss12/10/2010ñrrñrñr10/01/2000ksdifjsdifffffdd04/04/1998";
    String regex = "\\d{2}/\\d{2}/\\d{4}";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }

礼物,

12/07/2004
12/10/2010
10/01/2000
04/04/1998

这篇关于用随机字符串内的正则表达式匹配日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆