You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Vihan Pandey <vi...@gmail.com> on 2007/08/27 11:59:56 UTC

replaceregex issues

Hello,
        I have a rather strange issue while replacing strings from a
set of files. My objective is to replace occurences of :

../http://web1.foo.com with http://web1.foo.com
../../http://web1.foo.com with http://web1.foo.com
../../../http://web1.foo.com with http://web1.foo.com

and so on.

The ideal thing of course is to use a regex. i.e (../)* I did :

          <copy todir="${docroot}${release-version}/ct-html">

           <fileset dir="${deployroot}/ct-html/branches/${release-branch}">
                <include name="**/*.*"/>
           </fileset>

          <filterchain>
                <replaceregex pattern="../http" replace="http" flags="gi"/>
          </filterchain>

          </copy>

This works in replacing ../html by html without problems. However when I do :

          <copy todir="${docroot}${release-version}/ct-html">

           <fileset dir="${deployroot}/ct-html/branches/${release-branch}">
                <include name="**/*.*"/>
           </fileset>

          <filterchain>
                <replaceregex pattern="(../)*http" replace="http" flags="gi"/>
          </filterchain>

          </copy>

This fails.

If i do :

          <copy todir="${docroot}${release-version}/ct-html">

           <fileset dir="${deployroot}/ct-html/branches/${release-branch}">
                <include name="**/*.*"/>
           </fileset>

          <filterchain>
                <replaceregex pattern="(../)*http" replace="http" flags="gi"/>
          </filterchain>


          <filterchain>
                <replaceregex pattern="../http" replace="http" flags="gi"/>
          </filterchain>

          </copy>

This replaces the ../html by http but the ../../html ../../../html get
left out. The logical conclusion is my regex is wrong. i tried ^(../)*
but that does not help either.

Any suggestions?

Regards,

- vihan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: replaceregex issues

Posted by Vihan Pandey <vi...@gmail.com>.
On 3/12/08, neo anderson <ja...@yahoo.co.uk> wrote:
>
> How do I use shell script in ant? I try the following code, but it does not
> work.
>
> <project>
>        <target name="test">
>                <shellscript shell="bash">
>                        (echo "hi!";)
>                </shellscript>
>        </target>
> </project>

> Though it return build successfully. But there is nothing echo on the
> screen. What should I do if I want to run shell script command or use
> command (e.g., ) a bit like shell script in ant?

Can you try doing a (touch foo.bar) instead of the (echo "hi!";) and
see if an empty file gets created?

Regards,

- vihan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: replaceregex issues

Posted by neo anderson <ja...@yahoo.co.uk>.
How do I use shell script in ant? I try the following code, but it does not
work.

<project>
        <target name="test">
                <shellscript shell="bash">
                        (echo "hi!";)
                </shellscript>
        </target>
</project>

Though it return build successfully. But there is nothing echo on the
screen. What should I do if I want to run shell script command or use
command (e.g., ) a bit like shell script in ant?



> <!-- REPLACE SHELL SCRIPT -->
>                 <shellscript shell="bash">
>                         (for i in `find /path/to/my/files/ -name
> \*.html -print`;do cat $i | sed s/[..\/]*http/http/g > $i.tmp; cat
> $i.tmp > $i; rm -rf $i.tmp;done)
>                 </shellscript>
> 
>                 <shellscript shell="bash">
>                         (for i in `find /path/to/my/files/ -name
> \*.shtml -print`;do cat $i | sed s/[..\/]*http/http/g > $i.tmp; cat
> $i.tmp > $i; rm -rf $i.tmp;done)
>                 </shellscript>
> 
> 

-- 
View this message in context: http://www.nabble.com/replaceregex-issues-tp12344340p16002936.html
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: replaceregex issues

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
Hi, 

-----Original Message-----
From: Vihan Pandey [mailto:vihanpandey@gmail.com] 
Sent: Monday, August 27, 2007 4:35 PM
To: Ant Users List
Subject: Re: replaceregex issues

>
> <replaceregex pattern="[\.\./]{1,}(h.+)" replace="\1" flags="m"/>
>

/*
Thanks a million Gilbert !!! I REALLY appreciate all the effort you
have taken in thinking of this and testing it out :-) However i've
noticed something strange that the replaceregex task fails in
cretain(but not all) cases(if there are a VERY large number of files
nested in a deep directory structure and/or the files are a little
messed up)
*/

strange things = memory problems, error messages or
does the replacement not work anymore with some files ?

the regular expressions [\.\./]{1,}(h.+) means
the string ../ one or more times followed by the character h followed
by any character (not anything as dotall flag not set) one or more times
captured in a group and should work with the most implementations
of regexp; it does with ruby for example.

/*
We have developers working Windows, GNU/Linux and Mac OS X who check
in these files into a subversion respository(from which we check out
via an ant script then deploy). The result of this heterogeneous
development environment is a lot of ^M's and other control and escape
characters all over the place thus a nicely structured file with good
newlines becomes a one liner full of of junk characters and looked
messed up. This is not a problem as the webserver can read the file on
deployment.
*/

I'm SURE you'll get problems with those files someday in some way.
Even on several platform with different editors one has to create some
kind of style guide every developer has to follow, i.e.=
2 or 4 spaces instead of tabs, linefeeds all unixconform means LF
instead
of CR+LF when mixed platforms, path separators all unix = '/',and
finally
no endless one liners but well structured files as sometimes
humans have to read it too ;-)

/*
After a lot of thinking i decided to use sed in a shellscript via
sed(which in my experience is the best for such messed up files).
*/

two things when using shellscripts =

i would have recommend the use of the <shellscript> task of antcontrib
if you were using <exec> but i see you already use it :-)

keep in mind when using a shellscript you open a new
process, doesn't run in the VM of ant


Regards, Gilbert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: replaceregex issues

Posted by Vihan Pandey <vi...@gmail.com>.
>
> <replaceregex pattern="[\.\./]{1,}(h.+)" replace="\1" flags="m"/>
>

Thanks a million Gilbert !!! I REALLY appreciate all the effort you
have taken in thinking of this and testing it out :-) However i've
noticed something strange that the replaceregex task fails in
cretain(but not all) cases(if there are a VERY large number of files
nested in a deep directory structure and/or the files are a little
messed up).

We have developers working Windows, GNU/Linux and Mac OS X who check
in these files into a subversion respository(from which we check out
via an ant script then deploy). The result of this heterogeneous
development environment is a lot of ^M's and other control and escape
characters all over the place thus a nicely structured file with good
newlines becomes a one liner full of of junk characters and looked
messed up. This is not a problem as the webserver can read the file on
deployment.

After a lot of thinking i decided to use sed in a shellscript via
sed(which in my experience is the best for such messed up files).

Here is what i did :

<!-- REPLACE SHELL SCRIPT -->
                <shellscript shell="bash">
                        (for i in `find /path/to/my/files/ -name
\*.html -print`;do cat $i | sed s/[..\/]*http/http/g > $i.tmp; cat
$i.tmp > $i; rm -rf $i.tmp;done)
                </shellscript>

                <shellscript shell="bash">
                        (for i in `find /path/to/my/files/ -name
\*.shtml -print`;do cat $i | sed s/[..\/]*http/http/g > $i.tmp; cat
$i.tmp > $i; rm -rf $i.tmp;done)
                </shellscript>

This seems to work perfectly.

But again, Thanks a million Gilbert - i REALLY appreciate it :-) i
will surely use your method when i need it :-)

Thanks again!!!

Regards,

 -vihan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: replaceregex issues

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 


-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
Sent: Monday, August 27, 2007 1:43 PM
To: Ant Users List
Subject: RE: replaceregex issues

/*
ok, tested now =

<replaceregex pattern="[\.\./]{1,}(h.+)" replace="/1" flags="m"/>

works for me with ant 1.6.5 / jdk 1.4.2_08
*/


typo, has to be =

<replaceregex pattern="[\.\./]{1,}(h.+)" replace="\1" flags="m"/>

instead, the reference to the group has to be written with ' \ '


Regards, Gilbert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: replaceregex issues

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 




-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
Sent: Monday, August 27, 2007 1:19 PM
To: Ant Users List
Subject: RE: replaceregex issues

-----Original Message-----
From: Vihan Pandey [mailto:vihanpandey@gmail.com] 
Sent: Monday, August 27, 2007 12:00 PM
To: Ant Users List
Subject: replaceregex issues

/*

../http://web1.foo.com with http://web1.foo.com
../../http://web1.foo.com with http://web1.foo.com
../../../http://web1.foo.com with http://web1.foo.com

...

This replaces the ../html by http but the ../../html ../../../html get
left out. The logical conclusion is my regex is wrong. i tried ^(../)*
but that does not help either.

Any suggestions?


*/


ok, tested now =

<replaceregex pattern="[\.\./]{1,}(h.+)" replace="/1" flags="m"/>

works for me with ant 1.6.5 / jdk 1.4.2_08

Regards, Gilbert








---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: replaceregex issues

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 


Hi,

-----Original Message-----
From: Vihan Pandey [mailto:vihanpandey@gmail.com] 
Sent: Monday, August 27, 2007 12:00 PM
To: Ant Users List
Subject: replaceregex issues

/*

../http://web1.foo.com with http://web1.foo.com
../../http://web1.foo.com with http://web1.foo.com
../../../http://web1.foo.com with http://web1.foo.com

...

This replaces the ../html by http but the ../../html ../../../html get
left out. The logical conclusion is my regex is wrong. i tried ^(../)*
but that does not help either.

Any suggestions?


*/



untested, try something like =

<replaceregex pattern="[\.\./]{1,}(h.+)" replace="/1" flags="m"/>

or maybe even

<replaceregex pattern="(h.+)" replace="/1" flags="m"/>


Regards, Gilbert








---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org