public final class ExpressionMatchReplacer extends Object
Expression
-based replace-all functionality in the style
of Matcher.replaceAll(String)
.Modifier and Type | Method and Description |
---|---|
static Function<Matcher,String> |
get(Expression expression,
Mapping<String,?> variables)
Creates and returns a "match replacer" that is suitable for
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows) and implements the substitution through the given expression. |
static Function<Matcher,String> |
get(Expression expression,
Object... variableNamesAndValues) |
static Function<Matcher,String> |
parse(String spec)
Creates and returns a "match replacer" that is suitable for
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows) and implements the substitution through an Expression . |
static Function<Matcher,String> |
parse(String spec,
Mapping<String,?> variables,
Predicate<String> isValidVariableName)
Creates and returns a "match replacer" that is suitable for
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows) and implements the substitution through an Expression . |
static Function<Matcher,String> |
parse(String spec,
Object... variableNamesAndValues) |
static String |
replaceSome(Matcher matcher,
String spec)
Substitutes all matches of the matcher with the value of an expression.
|
public static String replaceSome(Matcher matcher, String spec) throws ParseException
The expression uses a single variable, "m"
, which is the Matcher
of the current match.
Example:
Matcher matcher = Pattern.compile(regex).matcher(input); String output = PatternUtil.replaceSome(matcher, "m.group.toUpperCase()");
If you plan to use the same expression for mutiple replaceSome(Matcher, String)
operations,
you can reduce the overhead of parsing the spec by calling parse(String)
(once) and PatternUtil.replaceSome(Matcher, FunctionWhichThrows)
(repeatedly).
spec
- Specifies the expression to use; when the expression evaluates to null
, then
the respective match is not replacedParseException
- A problem occurred when the spec was parsedThe expression syntax
public static Function<Matcher,String> parse(String spec) throws ParseException
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows)
and implements the substitution through an Expression
.
The expression uses a single variable, "m"
, which is the Matcher
of the current match.
Example:
FunctionmatchReplacer = ExpressionMatchReplacer.parse("m.group.toUpperCase()"); ... Matcher matcher = ...; System.out.println(PatternUtil.replaceSome(matcher, matchReplacer));
If you want to use more variables than just the matcher, use parse(String, Mapping, Predicate)
instead.
spec
- The text to be parsedParseException
- A problem occurred when the spec was parsedThe expression syntax
,
parse(String, Mapping, Predicate)
,
get(Expression, Mapping)
public static Function<Matcher,String> parse(String spec, Object... variableNamesAndValues) throws ParseException
ParseException
parse(String, Mapping, Predicate)
public static Function<Matcher,String> parse(String spec, Mapping<String,?> variables, Predicate<String> isValidVariableName) throws ParseException
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows)
and implements the substitution through an Expression
.
The expression uses the named variables, plus one more variable "m"
, which is the Matcher
of
the current match.
Example:
Function<Matcher, String> matchReplacer = ExpressionMatchReplacer.parse( "prefix + new StringBuilder(m.group).reverse()", Mappings.mapping("prefix", "pre-"), "prefix" ); ... Matcher matcher = ...; System.out.println(PatternUtil.replaceSome(matcher, matchReplacer));
If you want to use the same expression, but different variable values, then use get(Expression, Mapping)
instead.
spec
- The expression that will later be used for all substitutionsisValidVariableName
- Defines the names of all variables that the expression can usevariables
- The variables' values that will take effect when the expression is evaluated for each
matchParseException
- A problem occurred when the spec was parsedThe expression syntax
public static Function<Matcher,String> get(Expression expression, Object... variableNamesAndValues)
get(Expression, Mapping)
public static Function<Matcher,String> get(Expression expression, Mapping<String,?> variables)
PatternUtil.replaceSome(Matcher,
FunctionWhichThrows)
and implements the substitution through the given expression.
When the expression is evaluated, it gets an additional variable "m"
, which is the Matcher
of
the current match.
Example:
// Parsing the expression is relatively slow. Expression expression = new ExpressionEvaluator( "prefix", // <= This is "our" variable "m" // <= Also declare variable "m", which will automatically be available ).parse( "prefix + new StringBuilder(m.group).reverse()" ); // ... // Creating the match replacer with the actual variable values is fast. FunctionmatchReplacer = ExpressionMatchReplacer.get( expression, Mappings. mapping("prefix", "pre-") // <= pass the value for variable "prefix" ); // Now use the match replacer to substitute regex matches. Matcher matcher = ...; System.out.println(PatternUtil.replaceSome(matcher, matchReplacer));
expression
- Computes the replacement for each substitutionvariables
- The variables' values for the expressionThe expression syntax
Copyright © 2018 Arno Unkrig. All rights reserved.