commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Arno Unkrig (Jira)" <j...@apache.org>
Subject [jira] [Created] (TEXT-173) Pattern.replaceAll() replacement string extended to Java-like expression syntax
Date Fri, 04 Oct 2019 12:15:00 GMT
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)

Mime
View raw message