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.

Similar to regexp type mappers this task needs a supporting regular expression library and an implementation of org.apache.tools.ant.util.regexp.Regexp. Ant comes with implementations for the java.util.regex package of JDK 1.4, jakarta-regexp and jakarta-ORO, but you will still need the library itself.

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 <regularexpression> is used
replace The substition 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 occurances 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 occurance of a regular expression on each line, which is not easy to do when processing the file as a whole. Defaults to false. No

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 RegularExpression element to specify the regular expression. You can use this element to refer to a previously defined regular expression datatype instance.

<regularexpression id="id" pattern="expression" />
<regularexpression 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 substition pattern datatype instance.

<substitution id="id" pattern="expression" />
<substitution refid="id" />

Examples

<replaceregexp byline="true">
  <regularexpression expression="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


Copyright © 2001 Apache Software Foundation. All rights Reserved.