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] [Commented] (TEXT-173) Pattern.replaceAll() replacement string extended to Java-like expression syntax
Date Fri, 04 Oct 2019 12:28:00 GMT

    [ https://issues.apache.org/jira/browse/TEXT-173?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16944455#comment-16944455
] 

Arno Unkrig commented on TEXT-173:
----------------------------------

Here's relevant JAVADOC:
{quote}[ExpressionMatchReplacer|http://commons.unkrig.de/commons-text/apidocs/de/unkrig/commons/text/pattern/ExpressionMatchReplacer.html]
 [ExpressionEvaluator|http://commons.unkrig.de/commons-text/apidocs/de/unkrig/commons/text/expression/ExpressionEvaluator.html]
{quote}
There is also functionality to replace pattern matches within *character streams* (Readers
and Writers):
{quote}[PatternUtil.replaceAllFilterReader(Reader, Pattern, FunctionWhichThrows)|http://commons.unkrig.de/commons-text/apidocs/de/unkrig/commons/text/pattern/PatternUtil.html#replaceAllFilterReader-java.io.Reader-java.util.regex.Pattern-de.unkrig.commons.lang.protocol.FunctionWhichThrows-]
 [PatternUtil.replaceAllFilterWriter(Pattern, FunctionWhichThrows, Appendable)|http://commons.unkrig.de/commons-text/apidocs/de/unkrig/commons/text/pattern/PatternUtil.html#replaceAllFilterWriter-java.util.regex.Pattern-de.unkrig.commons.lang.protocol.FunctionWhichThrows-java.lang.Appendable-]
{quote}

> 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
>            Priority: Major
>
> 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