You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Arno Unkrig (Jira)" <ji...@apache.org> on 2019/10/04 12:15:00 UTC

[jira] [Created] (TEXT-173) Pattern.replaceAll() replacement string extended to Java-like expression syntax

Arno Unkrig created TEXT-173:
--------------------------------

             Summary: Pattern.replaceAll() replacement string extended to Java-like expression syntax
                 Key: TEXT-173
                 URL: https://issues.apache.org/jira/browse/TEXT-173
             Project: Commons Text
          Issue Type: Improvement
            Reporter: Arno Unkrig


I've developed a class that extends the {{Pattern.replaceAll()}} syntax from

{{   "Hello $1 $2"}}

to a simple, Java-like expression syntax, e.g.:

{{   "m.group.toUpperCase()"}}

Example client code:

{{   // Parse the expression.}}
{{   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()"}}
{{   );}}

{{   // ...}}

{{   // Create a match replacer with the actual variable values.}}
{{   Function<Matcher, String> matchReplacer = ExpressionMatchReplacer.get(}}
{{       expression,}}
{{       Mappings.<String, Object>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));}}

Would that be a neat extension to {{text.commons.apache.org}}?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)