/** * Returns a replacer will substitute all occurrences of a pattern * through applying a user-defined substitution model. * * @param model a Substitution object which is in charge for match substitution * @see Replacer */ public Replacer replacer(Substitution model) { return new Replacer(this, model); }
/** * Returns a replacer will substitute all occurrences of a pattern * through applying a user-defined substitution model. * * @param model a Substitution object which is in charge for match substitution * @see Replacer */ public Replacer replacer(Substitution model) { return new Replacer(this, model); }
/** * Returns a replacer of a pattern by specified perl-like expression. * Such replacer will substitute all occurrences of a pattern by an evaluated expression * ("$&" and "$0" will substitute by the whole match, "$1" will substitute by group#1, etc). * Example:<pre> * String text="The quick brown fox jumped over the lazy dog"; * Pattern word=new Pattern("\\w+"); * System.out.println(word.replacer("[$&]").replace(text)); * //prints "[The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]" * Pattern swap=new Pattern("(fox|dog)(.*?)(fox|dog)"); * System.out.println(swap.replacer("$3$2$1").replace(text)); * //prints "The quick brown dog jumped over the lazy fox" * Pattern scramble=new Pattern("(\\w+)(.*?)(\\w+)"); * System.out.println(scramble.replacer("$3$2$1").replace(text)); * //prints "quick The fox brown over jumped lazy the dog" * </pre> * * @param expr a perl-like expression, the "$&" and "${&}" standing for whole match, the "$N" and "${N}" standing for group#N, and "${Foo}" standing for named group Foo. * @see Replacer */ public Replacer replacer(String expr) { return new Replacer(this, expr); }
/** * Returns a replacer of a pattern by specified perl-like expression. * Such replacer will substitute all occurrences of a pattern by an evaluated expression * ("$&" and "$0" will substitute by the whole match, "$1" will substitute by group#1, etc). * Example:<pre> * String text="The quick brown fox jumped over the lazy dog"; * Pattern word=new Pattern("\\w+"); * System.out.println(word.replacer("[$&]").replace(text)); * //prints "[The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]" * Pattern swap=new Pattern("(fox|dog)(.*?)(fox|dog)"); * System.out.println(swap.replacer("$3$2$1").replace(text)); * //prints "The quick brown dog jumped over the lazy fox" * Pattern scramble=new Pattern("(\\w+)(.*?)(\\w+)"); * System.out.println(scramble.replacer("$3$2$1").replace(text)); * //prints "quick The fox brown over jumped lazy the dog" * </pre> * * @param expr a perl-like expression, the "$&" and "${&}" standing for whole match, the "$N" and "${N}" standing for group#N, and "${Foo}" standing for named group Foo. * @see Replacer */ public Replacer replacer(String expr) { return new Replacer(this, expr); }
/** * Makes a Replacer that replaces a literal String key in dict with the corresponding String value in dict. Doesn't * need escapes in the Strings it searches for (at index 0, 2, 4, etc.), but cannot search for the exact two * characters in immediate succession, backslash then capital E, because it finds literal Strings using * {@code \\Q...\\E}. Uses only default modes (not case-insensitive, and most other flags don't have any effect * since this doesn't care about "\\w" or other backslash-escaped special categories), but you can get the Pattern * from this afterwards and set its flags with its setFlags() method. The Strings this replaces with are the values, * and are also literal. If the Map this is given is a sorted Map of some kind or a (preferably) LinkedHashMap, then * the order search strings will be tried will be stable; the same is not necessarily true for HashMap. * @param dict a Map (hopefully with stable order) with search String keys and replacement String values * @return a Replacer that will act as a replacement table for the given Strings */ public static Replacer makeTable(Map<String, String> dict) { if(dict == null || dict.isEmpty()) return new Replacer(Pattern.compile("(.+)"), new DummySubstitution("\\1")); TableSubstitution tab = new TableSubstitution(new LinkedHashMap<String, String>(dict)); StringBuilder sb = new StringBuilder(128); sb.append("(?>"); for(String s : tab.dictionary.keySet()) { sb.append("\\Q"); sb.append(s); sb.append("\\E|"); } sb.setCharAt(sb.length() - 1, ')'); return new Replacer(Pattern.compile(sb.toString()), tab); }
/** * Makes a Replacer that replaces a literal String key in dict with the corresponding String value in dict. Doesn't * need escapes in the Strings it searches for (at index 0, 2, 4, etc.), but cannot search for the exact two * characters in immediate succession, backslash then capital E, because it finds literal Strings using * {@code \\Q...\\E}. Uses only default modes (not case-insensitive, and most other flags don't have any effect * since this doesn't care about "\\w" or other backslash-escaped special categories), but you can get the Pattern * from this afterwards and set its flags with its setFlags() method. The Strings this replaces with are the values, * and are also literal. If the Map this is given is a sorted Map of some kind or a (preferably) LinkedHashMap, then * the order search strings will be tried will be stable; the same is not necessarily true for HashMap. * @param dict a Map (hopefully with stable order) with search String keys and replacement String values * @return a Replacer that will act as a replacement table for the given Strings */ public static Replacer makeTable(Map<String, String> dict) { if(dict == null || dict.isEmpty()) return new Replacer(Pattern.compile("$"), new DummySubstitution("")); TableSubstitution tab = new TableSubstitution(new LinkedHashMap<String, String>(dict)); StringBuilder sb = new StringBuilder(128); sb.append("(?>"); for(String s : tab.dictionary.keySet()) { sb.append("\\Q"); sb.append(s); sb.append("\\E|"); } if(sb.length() > 3) sb.setCharAt(sb.length() - 1, ')'); else sb.append(')'); return new Replacer(Pattern.compile(sb.toString()), tab); }
/** * Makes a Replacer that replaces a literal String at index i in pairs with the String at index i+1. Doesn't need * escapes in the Strings it searches for (at index 0, 2, 4, etc.), but cannot search for the exact two characters * in immediate succession, backslash then capital E, because it finds literal Strings using {@code \\Q...\\E}. * Uses only default modes (not case-insensitive, and most other flags don't have any effect since this doesn't care * about "\\w" or other backslash-escaped special categories), but you can get the Pattern from this afterwards and * set its flags with its setFlags() method. The Strings this replaces with are at index 1, 3, 5, etc. and * correspond to the search String immediately before it; they are also literal. * @param pairs alternating search String, then replacement String, then search, replacement, etc. * @return a Replacer that will act as a replacement table for the given Strings */ public static Replacer makeTable(String... pairs) { if(pairs == null || pairs.length < 2) return new Replacer(Pattern.compile("$"), new DummySubstitution("")); TableSubstitution tab = new TableSubstitution(pairs); StringBuilder sb = new StringBuilder(128); sb.append("(?>"); for(String s : tab.dictionary.keySet()) { sb.append("\\Q"); sb.append(s); sb.append("\\E|"); } if(sb.length() > 3) sb.setCharAt(sb.length() - 1, ')'); else sb.append(')'); return new Replacer(Pattern.compile(sb.toString()), tab); }
/** * Makes a Replacer that replaces a literal String at index i in pairs with the String at index i+1. Doesn't need * escapes in the Strings it searches for (at index 0, 2, 4, etc.), but cannot search for the exact two characters * in immediate succession, backslash then capital E, because it finds literal Strings using {@code \\Q...\\E}. * Uses only default modes (not case-insensitive, and most other flags don't have any effect since this doesn't care * about "\\w" or other backslash-escaped special categories), but you can get the Pattern from this afterwards and * set its flags with its setFlags() method. The Strings this replaces with are at index 1, 3, 5, etc. and * correspond to the search String immediately before it; they are also literal. * @param pairs alternating search String, then replacement String, then search, replacement, etc. * @return a Replacer that will act as a replacement table for the given Strings */ public static Replacer makeTable(String... pairs) { if(pairs == null || pairs.length < 2) return new Replacer(Pattern.compile("(.+)"), new DummySubstitution("\\1")); TableSubstitution tab = new TableSubstitution(pairs); StringBuilder sb = new StringBuilder(128); sb.append("(?>"); for(String s : tab.dictionary.keySet()) { sb.append("\\Q"); sb.append(s); sb.append("\\E|"); } sb.deleteCharAt(sb.length() - 1); sb.append(')'); return new Replacer(Pattern.compile(sb.toString()), tab); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given user and their associated NounTrait. * @param message the message to transform; should contain "@" or "$" in it, at least, to be replaced * @param user the name of the user for cases where it can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, String user, NounTrait userTrait) { Replacer ur = new Replacer(userPattern, new BeingSubstitution(user, userTrait, true)); return ur.replace(message); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given user, their associated NounTrait, the given target, and their NounTrait. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the name of the user for cases where it can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the name of the target for cases where it can replace text like "^" or "^Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, String user, NounTrait userTrait, String target, NounTrait targetTrait) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(target, targetTrait, false)), ur = new Replacer(userPattern, new BeingSubstitution(user, userTrait, true)); return ur.replace(tr.replace(message)); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given group of users and that group's associated NounTrait. The NounTrait only matters * if it is first-person or second-person (in which case this uses the plural form) or if the Group contains one * member (in which case it uses any gendered pronouns specified by userTrait); it uses {@link NounTrait#GROUP} in * any other case. * @param message the message to transform; should contain "@" or "$" in it, at least, to be replaced * @param user a {@link Group} of users (a String array) for cases where it can replace text, like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, Group user, NounTrait userTrait) { Replacer ur = new Replacer(userPattern, new BeingSubstitution(userTrait, true, user.members)); return ur.replace(message); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given Group of users, that group's associated NounTrait, the given target, and their * NounTrait. The NounTrait for user only matters if it is first-person or second-person (in which case this uses * the plural form) or if the Group contains one member (in which case it uses any gendered pronouns specified by * userTrait); it uses {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the {@link Group} of users for cases where they can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the name of the target for cases where it can replace text like "^" or "^Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, Group user, NounTrait userTrait, String target, NounTrait targetTrait) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(target, targetTrait, false)), ur = new Replacer(userPattern, new BeingSubstitution(userTrait, true, user.members)); return ur.replace(tr.replace(message)); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given Group of users, that group's associated NounTrait, the given group of targets, and * that group's NounTrait. The NounTraits only matter if they are is first-person or second-person (in which case * this uses the plural form) or if a Group contains one member (in which case it uses any gendered pronouns * specified by userTrait or targetTrait); it uses {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the {@link Group} of users for cases where they can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the {@link Group} of targets for cases where they can replace text like "@" or "@Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, Group user, NounTrait userTrait, Group target, NounTrait targetTrait) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(targetTrait, false, target.members)), ur = new Replacer(userPattern, new BeingSubstitution(userTrait, true, user.members)); return ur.replace(tr.replace(message)); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given user, their associated NounTrait, the given target, and their NounTrait. Also * replaces the nth occurrence of "~" with the matching nth item in extra, so the first "~" is replaced with the * first item in extra, the second "~" with the second item, and so on until one is exhausted. * @param message the message to transform; should contain "@", "^", "$", or "~" in it, at least, to be replaced * @param user the name of the user for cases where it can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the name of the target for cases where it can replace text like "^" or "^Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @param extra an array or vararg of String where the nth item in extra will replace the nth occurrence of "~" * @return a String resulting from the processing of message */ public static String transform(CharSequence message, String user, NounTrait userTrait, String target, NounTrait targetTrait, String... extra) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(target, targetTrait, false)), ur = new Replacer(userPattern, new BeingSubstitution(user, userTrait, true)); String text = ur.replace(tr.replace(message)); if(extra != null && extra.length > 0) { for (int i = 0; i < extra.length; i++) { text = text.replaceFirst("~", extra[i]); } } return text; }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given user, that user's associated NounTrait, the given Group of targets, and that * group's NounTrait. The NounTrait for target only matters if it is first-person or second-person (in which case * this uses the plural form) or if the Group contains one member (in which case it uses any gendered pronouns * specified by targetTrait); it uses {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the name of the user for cases where it can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the {@link Group} of targets for cases where they can replace text like "@" or "@Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @return a String resulting from the processing of message */ public static String transform(CharSequence message, String user, NounTrait userTrait, Group target, NounTrait targetTrait) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(targetTrait, false, target.members)), ur = new Replacer(userPattern, new BeingSubstitution(user, userTrait, true)); return ur.replace(tr.replace(message)); }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given Group of users, that group's associated NounTrait, the given target, and their * NounTrait. Also replaces the nth occurrence of "~" with the matching nth item in extra, so the first "~" is * replaced with the first item in extra, the second "~" with the second item, and so on until one is exhausted. The * NounTrait for user only matters if it is first-person or second-person (in which case this uses the plural form) * or if the Group contains one member (in which case it uses any gendered pronouns specified by userTrait); it uses * {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the {@link Group} of users for cases where they can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the name of the target for cases where it can replace text like "^" or "^Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @param extra an array or vararg of String where the nth item in extra will replace the nth occurrence of "~" * @return a String resulting from the processing of message */ public static String transform(CharSequence message, Group user, NounTrait userTrait, String target, NounTrait targetTrait, String... extra) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(target, targetTrait, false)), ur = new Replacer(userPattern, new BeingSubstitution(userTrait, true, user.members)); String text = ur.replace(tr.replace(message)); if(extra != null && extra.length > 0) { for (int i = 0; i < extra.length; i++) { text = text.replaceFirst("~", extra[i]); } } return text; }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given Group of users, that group's associated NounTrait, the given group of targets, and * that group's NounTrait. Also replaces the nth occurrence of "~" with the matching nth item in extra, so the first * "~" is replaced with the first item in extra, the second "~" with the second item, and so on until one is * exhausted. The NounTraits only matter if they are is first-person or second-person (in which case * this uses the plural form) or if a Group contains one member (in which case it uses any gendered pronouns * specified by userTrait or targetTrait); it uses {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the {@link Group} of users for cases where they can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the {@link Group} of targets for cases where they can replace text like "@" or "@Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @param extra an array or vararg of String where the nth item in extra will replace the nth occurrence of "~" * @return a String resulting from the processing of message */ public static String transform(CharSequence message, Group user, NounTrait userTrait, Group target, NounTrait targetTrait, String... extra) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(targetTrait, false, target.members)), ur = new Replacer(userPattern, new BeingSubstitution(userTrait, true, user.members)); String text = ur.replace(tr.replace(message)); if(extra != null && extra.length > 0) { for (int i = 0; i < extra.length; i++) { text = text.replaceFirst("~", extra[i]); } } return text; }
/** * Takes message and replaces any of the special terms this recognizes, like @, ^, and $, with the appropriately- * conjugated terms for the given user, that user's associated NounTrait, the given Group of targets, and that * group's NounTrait. Also replaces the nth occurrence of "~" with the matching nth item in extra, so the first "~" * is replaced with the first item in extra, the second "~" with the second item, and so on until one is exhausted. * The NounTrait for target only matters if it is first-person or second-person (in which case this uses the plural * form) or if the Group contains one member (in which case it uses any gendered pronouns specified by targetTrait); * it uses {@link NounTrait#GROUP} in any other case. * @param message the message to transform; should contain "@", "^", or "$" in it, at least, to be replaced * @param user the name of the user for cases where it can replace text like "@" or "@Name" * @param userTrait the {@link NounTrait} enum that determines how user should be referred to * @param target the {@link Group} of targets for cases where they can replace text like "@" or "@Name" * @param targetTrait the {@link NounTrait} enum that determines how the target should be referred to * @param extra an array or vararg of String where the nth item in extra will replace the nth occurrence of "~" * @return a String resulting from the processing of message */ public static String transform(CharSequence message, String user, NounTrait userTrait, Group target, NounTrait targetTrait, String... extra) { Replacer tr = new Replacer(targetPattern, new BeingSubstitution(targetTrait, false, target.members)), ur = new Replacer(userPattern, new BeingSubstitution(user, userTrait, true)); String text = ur.replace(tr.replace(message)); if(extra != null && extra.length > 0) { for (int i = 0; i < extra.length; i++) { text = text.replaceFirst("~", extra[i]); } } return text; }