// Uncolored, plain source file:  trans2a.java
// <p> // An example file distributed with com.stevesoft.pat // and com.stevesoft.pat.apps // <p> // This software comes without express or implied warranty. // No claim is made about the suitability of this software for // any purpose and neither we nor SteveSoft shall be liable for // damages suffered by the user of this software. import com.stevesoft.pat.*; import java.io.*;
public class trans2a { public static void main(String[] args) { // We start as in trans.java, with something // that transforms the names of the seasons. Transformer t = new Transformer(true); t.add(new String[] { "s/\\bspring\\b/summer/", "s/\\bsummer\\b/fall/", "s/\\bfall\\b/winter/", "s/\\bwinter\\b/spring/", });
// But now we decide that we want this to stop // happening after the symbol "!" occurs in the text. // After that word occurs, we want the replacements to stop // occuring. We can do this by calling the special rule // ${ABORT}. t.add("s/!/${ABORT}/"); System.out.println(t.replaceAll("spring, summer, ! fall"));
// However, this may not be quite what we want. Perhaps // we only want to stop the substitutions temporarily -- say // after the word off, but start them again after the word on. // Is there a way to change which thing we're using to match // and replace on the fly? Yes... ReplaceRule.define("season_is_on",t); ReplaceRule.define("season_is_off",new Regex("\\bon\\b","$&${=season_is_on}")); t.add("s/\\boff\\b/$&${=season_is_off}/"); System.out.println(t.replaceAll("spring, summer, off, fall, winter, on, fall")); } }