Bug 51167

Summary: Support string replacement as part of property expansion
Product: Ant Reporter: Keith D Gregory <kdgregory>
Component: CoreAssignee: Ant Notifications List <notifications>
Status: NEW ---    
Severity: enhancement    
Priority: P2    
Version: nightly   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Patch for 'replaceAll:' property prefix (against trunk as-of right now)

Description Keith D Gregory 2011-05-07 17:11:44 UTC
Created attachment 26968 [details]
Patch for 'replaceAll:' property prefix (against trunk as-of right now)

It is occasionally useful to take one property value and transform it with a simple string replace. For example, if you have a comma-delimited list of locales, you might want to generate a fileset from it (this example shows a BASH-style substitution; see below):

    <property name="locales" value="en_US,en_GB,en_CA,fr_FR,fr_CA"/>
    <property name="locale.regex" value="${locales/,/|}"/>

    <fileset dir="${resource.dir}">
        <filename regex="${locale.regex}.txt"/>
    </fileset>

It's currently possible to do this using the ant-contrib PropertyRegex task. However, using that task requires getting the ant-contrib JARfile and adding it to your classpath.

While I think the BASH-style expansions are cleaner, in keeping with current practice I suggest adding a "replaceAll:" prefix:

    <property name="locale.regex" value="${replaceAll:locales/,/|}"/>

I've attached a patch that implements this, with tests and changes to docs.
Comment 1 Matt Benson 2011-05-08 03:59:41 UTC
Seen http://ant.apache.org/antlibs/props/index.html ?

The stringops PropertyEvaluator implements bash style string handling.