4.12 Week 12 --  Networking4 Operator Precedence and Associativity4.10 Week 10 -- I/O  4.11 Week 11 -- Regular Expressions

4.11 Week 11 -- Regular Expressions

4.11.1 Reading and Web Sites

4.11.2 Notes

  1. Regular Expressions
  2. Saving sub-expressions
  3. Examples:
    1. Jgrep
      /* Louis Taber  PCC  4/9/2003  */
      /* regular expression demo program */
      
      import java.util.*;
      import java.util.regex.*;
      import java.io.*;
      
      class Jgrep
      {
      public static void main( String [] args)
        {
        BufferedReader Input = 
              new BufferedReader( 
                new InputStreamReader( System.in ) );
        Pattern clpattern = Pattern.compile( ".*" );
        Matcher matcher;
      
        // Process command line arguments.
      
        if( args.length <= 0 )
          {
          System.out.println(
             "Usage: java Jgrep regular_expression [input_file]\n" );
          System.exit(1);
          }
      
        if( args.length >= 1 )
          {
          clpattern = Pattern.compile( args[0].trim() );
          }
      
        if( args.length >= 2 )
          {
          try
            {
            Input = new BufferedReader(
                      new FileReader(args[1].trim()));
            }
          catch( FileNotFoundException e )
            {
            System.out.println( "Input file not found:\n" + e );
            }
          }
      
        // Check for match in input line 
        // with regular expression
      
        String input_string; 
      
        try
          {  
          while( ( input_string = Input.readLine()) != null )
            {
            matcher = clpattern.matcher( input_string );
            if( matcher.find( ) )
              {
              System.out.println("Match: " +  input_string );
              // Check for groups and print out groups if they exist
              if( matcher.groupCount() >= 1 )
                {
                System.out.println(
                     "    groupCount(): " +  matcher.groupCount() 
                                      );
                for( int i=0; i<=matcher.groupCount(); i++ ) 
                  {
                  System.out.println(
                       "    Group "+i+": " +  matcher.group(i) 
                                        );
                  }  // for
                }  // if 
              }  // if
            }  //  while
          }  // try
        catch( IOException e )
          {
          System.err.println( "File close() error:\n" + e );
          }  
        System.exit(0);
        }
      }
      

4.11.3 Assignments

None.


Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site

4.12 Week 12 --  Networking4 Operator Precedence and Associativity4.10 Week 10 -- I/O  4.11 Week 11 -- Regular Expressions