REReplace

Description

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

REReplace currently uses the jakarta-oro regular expression (org.apache.oro.text.regex) package, and hence the org.apache.oro.text.regex Perl5 regular expression syntax. (See http://jakarta.apache.org/oro.)

The replaceWith pattern can contain references to subexpressions within the find pattern, which makes it possible to use fragments of the original text in the new text.

Note that the regular expression strings are being run through an XML parse and an Ant interpretation, so you'll need to use the proper escaping (and you are able to use Ant ${properties}).

Parameters

Attribute Description Required
file The file in which the substitution should occur. Exactly one of the two.
dir The base directory to use when substituting in multiple files.
find The pattern to find. Yes, unless a nested find element is used.
replaceWith The string or regular expression to replace matches of the find value with. When omitted, an empty string ("") is used. No
skipLocked When true ("yes","on","true"), files that are not writable will be skipped. When false ("no","off","false"), files that are not writable will cause a BuildException to be thrown. When omitted, a true value is assumed. No
includes Comma separated list of patterns of files that must be included. All files are included when omitted. No
includesfile The name of an includes file. Each line of this file is taken to be an include pattern. No
excludes Comma separated list of patterns of files that must be excluded. No files (except default excludes) are excluded when omitted. No
excludesfile The name of an excludes file. Each line of this file is taken to be an exclude pattern No
defaultexcludes Indicates whether default excludes should be used or not ("yes"/"no"). Default excludes are used when omitted. No

Examples

  <rereplace file="${src}/index.html" find="foo" replaceWith="bar" />

replaces occurrences of the string "foo" with the string "bar", in the file ${src}/index.html.

  <rereplace file="${src}/index.html" find="f[aeiou]o" replaceWith="bar" />

replaces occurrences of the string "fao", "feo", "fio", "foo" and "fuo" with the string "bar", in the file ${src}/index.html.

  <rereplace file="${src}/index.html" find="f([aeiou])o" replaceWith="b$$1r" />

replaces occurrences of the string "fao" with "bar", "feo" with "ber", "fio" with "bir", "foo" with "bor", and "fuo" with "bur", in the file ${src}/index.html. (Note that we had to use $$ to generate the literal $, since Ant treats $ as special character.)

Parameters specified as nested elements

This task forms an implicit FileSet and supports all attributes of <fileset> as well as the nested <include>, <exclude> and <patternset> elements.

You can also use nested elements to specify the find and replaceWith patterns.

Examples

  
<rereplace dir="${src}" replaceWith="bar">
  <include name="**/*.html" />
  <find><![CDATA[multi line
pattern]]></find>
</rereplace>

replaces occurrences of the string "multi line\npattern" with the string "bar", in all HTML files in the directory ${src}.Where \n is the platform specific line separator.

  
<rereplace dir="${src}">
  <include name="**/*.html" />
  <find><![CDATA[multi line
pattern]]></find>
  <replaceWith>bar</replaceWith>
</rereplace>

does the same thing.