Source of RegexMatches.java


  1: // Fig. 29.24: RegexMatches.java
  2: // Demonstrating Classes Pattern and Matcher.
  3: import java.util.regex.Matcher;
  4: import java.util.regex.Pattern;

  6: public class RegexMatches
  7: {
  8:    public static void main( String args[] )
  9:    {
 10:       // create regular expression
 11:       Pattern expression = 
 12:          Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" );
 13:       
 14:       String string1 = "Jane's Birthday is 05-12-75\n" +
 15:          "Dave's Birthday is 11-04-68\n" +
 16:          "John's Birthday is 04-28-73\n" +
 17:          "Joe's Birthday is 12-17-77";

 19:       // match regular expression to string and print matches
 20:       Matcher matcher = expression.matcher( string1 );
 21:         
 22:       while ( matcher.find() )
 23:          System.out.println( matcher.group() );
 24:    } // end main
 25: } // end class RegexMatches


 28: /*
 29:  **************************************************************************
 30:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 31:  * Pearson Education, Inc. All Rights Reserved.                           *
 32:  *                                                                        *
 33:  * DISCLAIMER: The authors and publisher of this book have used their     *
 34:  * best efforts in preparing the book. These efforts include the          *
 35:  * development, research, and testing of the theories and programs        *
 36:  * to determine their effectiveness. The authors and publisher make       *
 37:  * no warranty of any kind, expressed or implied, with regard to these    *
 38:  * programs or to the documentation contained in these books. The authors *
 39:  * and publisher shall not be liable in any event for incidental or       *
 40:  * consequential damages in connection with, or arising out of, the       *
 41:  * furnishing, performance, or use of these programs.                     *
 42:  **************************************************************************
 43: */