Source of RegexSubstitution.java


  1: // Fig. 29.23: RegexSubstitution.java
  2: // Using methods replaceFirst, replaceAll and split.

  4: public class RegexSubstitution
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       String firstString = "This sentence ends in 5 stars *****";
  9:       String secondString = "1, 2, 3, 4, 5, 6, 7, 8";
 10:          
 11:       System.out.printf( "Original String 1: %s\n", firstString );

 13:       // replace '*' with '^'
 14:       firstString = firstString.replaceAll( "\\*", "^" );

 16:       System.out.printf( "^ substituted for *: %s\n", firstString );

 18:       // replace 'stars' with 'carets'
 19:       firstString = firstString.replaceAll( "stars", "carets" );

 21:       System.out.printf(
 22:          "\"carets\" substituted for \"stars\": %s\n", firstString );

 24:       // replace words with 'word'
 25:       System.out.printf( "Every word replaced by \"word\": %s\n\n",
 26:          firstString.replaceAll( "\\w+", "word" ) );

 28:       System.out.printf( "Original String 2: %s\n", secondString );

 30:       // replace first three digits with 'digit'     
 31:       for ( int i = 0; i < 3; i++ )
 32:          secondString = secondString.replaceFirst( "\\d", "digit" );

 34:       System.out.printf( 
 35:          "First 3 digits replaced by \"digit\" : %s\n", secondString );
 36:       String output = "String split at commas: [";

 38:       String[] results = secondString.split( ",\\s*" ); // split on commas

 40:       for ( String string : results )
 41:          output += "\"" + string + "\", "; // output results

 43:       // remove the extra comma and add a bracket
 44:       output = output.substring( 0, output.length() - 2 ) + "]";
 45:       System.out.println( output );
 46:    } // end main
 47: } // end class RegexSubstitution

 49: /*
 50:  **************************************************************************
 51:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 52:  * Pearson Education, Inc. All Rights Reserved.                           *
 53:  *                                                                        *
 54:  * DISCLAIMER: The authors and publisher of this book have used their     *
 55:  * best efforts in preparing the book. These efforts include the          *
 56:  * development, research, and testing of the theories and programs        *
 57:  * to determine their effectiveness. The authors and publisher make       *
 58:  * no warranty of any kind, expressed or implied, with regard to these    *
 59:  * programs or to the documentation contained in these books. The authors *
 60:  * and publisher shall not be liable in any event for incidental or       *
 61:  * consequential damages in connection with, or arising out of, the       *
 62:  * furnishing, performance, or use of these programs.                     *
 63:  **************************************************************************
 64: */