Java Tutorial/Development/Regular Expressions

Материал из Java эксперт
Версия от 15:28, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Pattern match: J.*\\d[-\\d\\d-\\d\\d

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass
{
   public static void main( String args[] )
   {
      // create regular expression
      Pattern expression = 
         Pattern.rupile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" );
      
      String string1 = "Jack"s Birthday is 05-12-75\n" +
         "Joe"s Birthday is 11-04-68\n" +
         "Tom"s Birthday is 04-28-73\n" +
         "Lee" +
         "s Birthday is 12-17-77";
      // match regular expression to string and print matches
      Matcher matcher = expression.matcher( string1 );
        
      while ( matcher.find() )
         System.out.println( matcher.group() );
   }
}



Jack"s Birthday is 05-12-75
Joe"s Birthday is 11-04-68


Replace all words with another string

public class MainClass
{
   public static void main( String args[] )
   {
      String firstString = "This sentence ends in 5 stars *****";
      String secondString = "1, 2, 3, 4, 5, 6, 7, 8";
         
      System.out.printf( "Original String 1: %s\n", firstString );
      // replace words with "word"
      System.out.printf( "Every word replaced by \"word\": %s\n\n",
         firstString.replaceAll( "\\w+", "word" ) );
      System.out.printf( "Original String 2: %s\n", secondString );
   }
}



Original String 1: This sentence ends in 5 stars *****
Every word replaced by "word": word word word word word word *****
Original String 2: 1, 2, 3, 4, 5, 6, 7, 8


Replace first three digits with "digit"

public class MainClass
{
   public static void main( String args[] )
   {
      String firstString = "This sentence ends in 5 stars *****";
      String secondString = "1, 2, 3, 4, 5, 6, 7, 8";
         
      System.out.printf( "Original String 1: %s\n", firstString );
      // replace first three digits with "digit"     
      for ( int i = 0; i < 3; i++ )
         secondString = secondString.replaceFirst( "\\d", "digit" );
      System.out.printf( 
         "First 3 digits replaced by \"digit\" : %s\n", secondString );
   }
}



Original String 1: This sentence ends in 5 stars *****
First 3 digits replaced by "digit" : digit, digit, digit, 4, 5, 6, 7, 8


Replace one string with another string

public class MainClass
{
   public static void main( String args[] )
   {
      String firstString = "This sentence ends in 5 stars *****";
         
      System.out.printf( "Original String 1: %s\n", firstString );
      // replace "stars" with "carets"
      firstString = firstString.replaceAll( "stars", "carets" );
      System.out.printf(
         "\"carets\" substituted for \"stars\": %s\n", firstString );
   }
}



Original String 1: This sentence ends in 5 stars *****
"carets" substituted for "stars": This sentence ends in 5 carets *****


Replace "*" with "^"

public class MainClass
{
   public static void main( String args[] )
   {
      String firstString = "This sentence ends in 5 stars *****";
        
      System.out.printf( "Original String 1: %s\n", firstString );
      // replace "*" with "^"
      firstString = firstString.replaceAll( "\\*", "^" );
      System.out.printf( "^ substituted for *: %s\n", firstString );
   }
}



Original String 1: This sentence ends in 5 stars *****
^ substituted for *: This sentence ends in 5 stars ^^^^^


Split string with comma

public class MainClass
{
   public static void main( String args[] )
   {
      String secondString = "1, 2, 3, 4, 5, 6, 7, 8";
         
      String output = "String split at commas: [";
      String[] results = secondString.split( ",\\s*" ); // split on commas
      for ( String string : results )
         output += "\"" + string + "\", ";
      output = output.substring( 0, output.length() - 2 ) + "]";
      System.out.println( output );
   }
}



String split at commas: ["1", "2", "3", "4", "5", "6", "7", "8"]


Validate Address

public class MainClass
{
   public static void main( String[] args )
   {
       System.out.println(validateAddress("123, Street"));
   }
   // validate address
   public static boolean validateAddress( String address )
   {
      return address.matches( 
         "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
   } // end method validateAddress
   
}





Validate city and state

public class MainClass
{
   public static void main( String[] args )
   {
       System.out.println(validateCity("Street"));
       System.out.println(validateState("CA"));
   }
   // validate city
   public static boolean validateCity( String city )
   {
      return city.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
   } // end method validateCity
   // validate state
   public static boolean validateState( String state )
   {
      return state.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) ;
   } // end method validateState
   
}





Validate Phone

public class MainClass
{
   public static void main( String[] args )
   {
       System.out.println(validatePhone("123-456-1111"));
   }
   public static boolean validatePhone( String phone )
   {
      return phone.matches( "[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" );
   } // end method validatePhone
}





Validate the first name and last name

public class MainClass
{
   public static void main( String[] args )
   {
       System.out.println(validateFirstName("Tom"));
       System.out.println(validateLastName("Tom"));
   }
   // validate first name
   public static boolean validateFirstName( String firstName )
   {
      return firstName.matches( "[A-Z][a-zA-Z]*" );
   } // end method validateFirstName
   // validate last name
   public static boolean validateLastName( String lastName )
   {
      return lastName.matches( "[a-zA-z]+([ "-][a-zA-Z]+)*" );
   } // end method validateLastName
   
}





Validate Zip

public class MainClass
{
   public static void main( String[] args )
   {
       System.out.println(validateZip("123456"));
   }
   // validate zip
   public static boolean validateZip( String zip )
   {
      return zip.matches( "\\d{5}" );
   } 
}