How to pull out a substring in Ant

Viewed 41768

Is there a way to pull a substring from an Ant property and place that substring into it's own property?

6 Answers

You could try using PropertyRegex from Ant-Contrib.

   <propertyregex property="destinationProperty"
              input="${sourceProperty}"
              regexp="regexToMatchSubstring"
              select="\1"
              casesensitive="false" />

I guess an easy vanilla way to do this is:

<loadresource property="destinationProperty">
    <concat>${sourceProperty}</concat>
    <filterchain>
        <replaceregex pattern="regexToMatchSubstring" replace="\1" />
    </filterchain>
</loadresource>

I would go with the brute force and write a custom Ant task:

public class SubstringTask extends Task {

    public void execute() throws BuildException {
        String input = getProject().getProperty("oldproperty");
        String output = process(input);
        getProject().setProperty("newproperty", output);
    }
}

What's left it to implement the String process(String) and add a couple of setters (e.g. for the oldproperty and newproperty values)

Related