You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Aurelien Pernoud <ap...@sopragroup.com> on 2003/03/13 17:01:11 UTC

Rename with regexp ?

Hi there,

I've been looking in the faq and in mailing-list archive but I don't find a
way to do this simple (?) thing.

I'd like to rename files this way :

file.extension      		 -> file.extension
file.version.extension         -> file_version.extension
file.foo.version.extension     -> file_foo_version.extension

In fact a simple replace of all "." (except last one) by "_" char.

So I tried using regexp extensions, without succes, I can only get the last
pattern match in the result.

I saw a related trouble there
http://www.mail-archive.com/ant-user@jakarta.apache.org/msg11158.html

But the answer was (at that time : 12/2001) "not possible".

I'm stuck :(

Thx for any pointer (even if it is "no you can't"),
Aurelien Pernoud.

PS : Please CC me if you have the answer, I'm not on this mailing-list.


Re: Rename with regexp ?

Posted by peter reilly <pe...@corvil.com>.
You can use a custom mapper.

Something like:

package my.package;

import org.apache.tools.ant.*;
import org.apache.tools.ant.util.*;

public class CustomMapper
    implements FileNameMapper
{
    public void setFrom(String from) {}
    public void setTo(String to) {}
    
    public String[] mapFileName(String sourceFileName) {
        String c = sourceFileName.replace('.', '_');
        int index = c.lastIndexOf('_');
        if (index != -1)
            c = c.substring(0, index) + "." + c.substring(index+1);
        return new String[]{c};
    }
}

and call it like:

    <target name="t">
        <mkdir dir="test"/>
        <touch file="test/a.b.c.d"/>
        <copy todir=".">
            <fileset dir="test"/>
            <mapper classname="my.package.CustomMapper"
                    classpathref="path"/>
        </copy>
    </target>

Peter.

On Thursday 13 March 2003 16:01, Aurelien Pernoud wrote:
> Hi there,
>
> I've been looking in the faq and in mailing-list archive but I don't find a
> way to do this simple (?) thing.
>
> I'd like to rename files this way :
>
> file.extension      		 -> file.extension
> file.version.extension         -> file_version.extension
> file.foo.version.extension     -> file_foo_version.extension
>
> In fact a simple replace of all "." (except last one) by "_" char.
>
> So I tried using regexp extensions, without succes, I can only get the last
> pattern match in the result.
>
> I saw a related trouble there
> http://www.mail-archive.com/ant-user@jakarta.apache.org/msg11158.html
>
> But the answer was (at that time : 12/2001) "not possible".
>
> I'm stuck :(
>
> Thx for any pointer (even if it is "no you can't"),
> Aurelien Pernoud.
>
> PS : Please CC me if you have the answer, I'm not on this mailing-list.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org