java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4088
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use ReplaceRegExp task E-mail
User Rating: / 14
PoorBest 

ReplaceRegExp

Description

ReplaceRegExp is a directory based task for replacing the occurrence of a given regular expression with a substitution pattern in a selected file or set of files.

The output file is only written if it differs from the existing file. This prevents spurious rebuilds based on unchanged files which have been regenerated by this task.

Similar to regexp type mappers this task needs a supporting regular expression library and an implementation of org.apache.tools.ant.util.regexp.Regexp. See details in the documentation of the Regexp Type.

Parameters

Attribute Description Required
file file for which the regular expression should be replaced. Yes if no nested <fileset> is used
match The regular expression pattern to match in the file(s) Yes, if no nested <regexp> is used
replace The substitution pattern to place in the file(s) in place of the regular expression. Yes, if no nested <substitution> is used
flags The flags to use when matching the regular expression. For more information, consult the Perl5 syntax
g : Global replacement. Replace all occurrences found
i : Case Insensitive. Do not consider case in the match
m : Multiline. Treat the string as multiple lines of input, using "^" and "$" as the start or end of any line, respectively, rather than start or end of string.
s : Singleline. Treat the string as a single line of input, using "." to match any character, including a newline, which normally, it would not match.
No
byline Process the file(s) one line at a time, executing the replacement on one line at a time (true/false). This is useful if you want to only replace the first occurrence of a regular expression on each line, which is not easy to do when processing the file as a whole. Defaults to false. No
encoding The encoding of the file. since Ant 1.6 No - defaults to default JVM encoding

Examples

  <replaceregexp file="${src}/build.properties"
                         match="OldProperty=(.*)"
                         replace="NewProperty=\1"

                         byline="true"/>

replaces occurrences of the property name "OldProperty" with "NewProperty" in a properties file, preserving the existing value, in the file ${src}/build.properties

Parameters specified as nested elements

This task supports a nested FileSet element.

This task supports a nested Regexp element to specify the regular expression. You can use this element to refer to a previously defined regular expression datatype instance.

<regexp id="id" pattern="alpha(.+)beta"/>
<regexp refid="id"/>

This task supports a nested Substitution element to specify the substitution pattern. You can use this element to refer to a previously defined substitution pattern datatype instance.

<substitution id="id" expression="beta\1alpha"/>
<substitution refid="id"/>

Examples

<replaceregexp byline="true">
  <regexp pattern="OldProperty=(.*)"/>
  <substitution expression="NewProperty=\1"/>

  <fileset dir=".">
   <includes="*.properties"/>
  </fileset>
 </replaceregexp>

replaces occurrences of the property name "OldProperty" with "NewProperty" in a properties file, preserving the existing value, in all files ending in .properties in the current directory

<replaceregexp match="\s+" replace=" " flags="g" byline="true">
    <fileset dir="${html.dir}" includes="**/*.html"/>
</replaceregexp>

replaces all whitespaces (blanks, tabs, etc) by one blank remaining the line separator. So with input

<html>    <body>
<<TAB>><h1>    T E S T   </h1>  <<TAB>>    
<<TAB>> </body></html>

would converted to
<html> <body>
 <h1> T E S T </h1> </body></html>

Source: Apache Ant


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.