Java Regular Expression match date String

Description

Java Regular Expression match date String


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{
   public static void main(String[] args)
   {/*  w  ww  .j  a v a 2s. co  m*/
      // create regular expression
      Pattern expression = 
         Pattern.compile("J.*\\d[0-35-9]-\\d\\d-\\d\\d");
      
      String string1 = "Java's Birthday is 05-12-75\n" +
         "HTML's Birthday is 11-04-68\n" +
         "Javascript's Birthday is 04-28-73\n" +
         "Json's Birthday is 12-17-1997";

      // match regular expression to string and print matches
      Matcher matcher = expression.matcher(string1);
        
      while (matcher.find())
         System.out.println(matcher.group());
   } 
}

The following table lists the predefined Java Regular expression character classes:

CharacterMatches
\d any digit
\D any non-digit
\w any word character
\W any nonword character
\s any white-space character
\S any non-whitespace character



PreviousNext

Related