![]() | ![]() | ![]() | 4.11 Week 11 -- Regular Expressions |
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); } }
None.
![]() | ![]() | ![]() | 4.11 Week 11 -- Regular Expressions |