/*** * substituteExample.java,v 1.2 1997/06/23 11:10:35 dfs Exp * * This is a test program demonstrating the use of the Util.substitute() * method. * * Copyright 1997 ORO, Inc. All rights reserved. ***/ import com.oroinc.text.regex.*; public final class substituteExample { /*** * A good way for you to understand the substitute() method is to play around * with it by using this test program. The program takes 3 to 5 arguments * as follows: * java substituteExample * regex substitution input [sub limit] [interpolation limit] * regex - A regular expression used to find parts of the input to be * substituted. * sub limit - An optional argument limiting the number of substitutions. * If no limit is given, the limit used is Util.SUBSTITUTE_ALL. * input - A string to be used as input for substitute(). * interpolation limit - An optional argument limiting the number of * interpolations performed. * * Try the following command line for a simple example of subsitute(). * It changes (2,3) to (3,2) in the input. * java substituteExample '\(2,3\)' '(3, 2)' '(1,2) (2,3) (4,5)' * * The following command line shows the substitute limit at work. It * changed the first four 1's in the input to 4's. * java substituteExample '1' '4' '381298175 1111' * * The next command line shows how to use interpolations. Suppose we * want to reverse the coordinates of the first 3 entries in the input * and then have all the rest of the coordinates be equal to the new 3rd * entry: java substituteExample '\((\d+),(\d+)\)' '($2,$1)' '(1,2) (2,3) (4,5) (8,8) (9,2)' 5 3 * ***/ public static final void main(String args[]) { int limit, interps, i; PatternMatcher matcher = new Perl5Matcher(); Pattern pattern = null; PatternCompiler compiler = new Perl5Compiler(); String regularExpression, input, sub, result; // Make sure there are sufficient arguments if(args.length < 3) { System.err.println("Usage: substituteExample regex substitution " + "input [sub limit] [interpolation limit]"); System.exit(1); } limit = Util.SUBSTITUTE_ALL; interps = Util.INTERPOLATE_ALL; regularExpression = args[0]; sub = args[1]; input = args[2]; if(args.length > 3) limit = Integer.parseInt(args[3]); if(args.length > 4) interps = Integer.parseInt(args[4]); try { pattern = compiler.compile(regularExpression); System.out.println("substitute regex: " + regularExpression); } catch(MalformedPatternException e){ System.err.println("Bad pattern."); System.err.println(e.getMessage()); System.exit(1); } // Perform substitution and print result. result = Util.substitute(matcher, pattern, sub, input, limit, interps); System.out.println("result: " + result); } }