简体   繁体   English

用随机字符串内的正则表达式匹配日期

[英]Matching dates with Regex inside of a random string

I am trying to do this in Java : 我试图在Java执行此操作:

I receive this kind of string 我收到这种字符串

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

Then I have to find one or more dates inside that string, date format: dd/mm/yyyy 然后,我必须在该字符串中找到一个或多个日期,日期格式为: dd/mm/yyyy

Finally I have to copy to another string dates matched: "12/07/2004 12/10/2010 10/01/2000 04/04/1998" 最后,我必须将匹配的另一个日期复制到: "12/07/2004 12/10/2010 10/01/2000 04/04/1998"

PD: I'm using this website http://regexpal.com/ to test if works. PD:我正在使用此网站http://regexpal.com/进行测试。 I tried some website regex and anyone worked for me. 我尝试了一些网站正则表达式 ,有人为我工作。

You can separate the validity of the date with the extracted content. 您可以将日期的有效性与提取的内容分开。

To extract the dates: 要提取日期:

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

Check here at fiddle: http://fiddle.re/fa0bf 在小提琴上查看此处: http : //fiddle.re/fa0bf

Code: 码:

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

Gives, 给人,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM