/*** * splitExample.java,v 1.2 1997/06/23 11:10:35 dfs Exp * * This is a test program demonstrating the use of the Util.split() method. * * Copyright 1997 ORO, Inc. All rights reserved. ***/ import java.util.*; import com.oroinc.text.regex.*; public final class splitExample { /*** * A good way for you to understand the split() method is to play around * with it by using this test program. The program takes 2 or 3 arguments * as follows: * java splitExample regex input [split limit] * regex - A regular expression used to split the input. * input - A string to be used as input for split(). * split limit - An optional argument limiting the size of the list returned * by split(). If no limit is given, the limit used is * Util.SPLIT_ALL. Setting the limit to 1 generally doesn't * make any sense. * * Try the following two command lines to see how split limit works: * java splitExample '[:|]' '1:2|3:4' * java splitExample '[:|]' '1:2|3:4' 3 * ***/ public static final void main(String args[]) { int limit, i; String regularExpression, input; Vector results; Pattern pattern = null; PatternMatcher matcher; PatternCompiler compiler; // Make sure there are sufficient arguments if(args.length < 2) { System.err.println("Usage: splitExample regex input [split limit]"); System.exit(1); } regularExpression = args[0]; input = args[1]; if(args.length > 2) limit = Integer.parseInt(args[2]); else limit = Util.SPLIT_ALL; // Create Perl5Compiler and Perl5Matcher instances. compiler = new Perl5Compiler(); matcher = new Perl5Matcher(); // Attempt to compile the pattern. If the pattern is not valid, // report the error and exit. try { pattern = compiler.compile(regularExpression); System.out.println("split regex: " + regularExpression); } catch(MalformedPatternException e){ System.err.println("Bad pattern."); System.err.println(e.getMessage()); System.exit(1); } // Split the input and print the resulting list. System.out.println("split results: "); results = Util.split(matcher, pattern, input, limit); for(i = 0; i < results.size(); i++){ System.out.println("item " + i + ": " + (String)results.elementAt(i)); } } }