Matching dates with Regex inside of a random string

后端 未结 1 1963
后悔当初
后悔当初 2021-01-15 22:40

I am trying to do this in Java:

I receive this kind of string

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


        
相关标签:
1条回答
  • 2021-01-15 23:03

    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

    Code:

     String input = "12/07/2004ffffdsss12/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
    
    0 讨论(0)
提交回复
热议问题