1 | <html>
|
---|
2 |
|
---|
3 | <head>
|
---|
4 | <meta http-equiv="Content-Language" content="en-us">
|
---|
5 | <title>ReplaceRegExp Task</title>
|
---|
6 | <link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
|
---|
7 | </head>
|
---|
8 | <body>
|
---|
9 |
|
---|
10 | <h2><a name="replaceregexp">ReplaceRegExp</a></h2>
|
---|
11 | <h3>Description</h3>
|
---|
12 | <p>ReplaceRegExp is a directory based task for replacing the
|
---|
13 | occurrence of a given regular expression with a substitution pattern
|
---|
14 | in a selected file or set of files.</p>
|
---|
15 |
|
---|
16 | <p>The output file is only written if it differs from the existing
|
---|
17 | file. This prevents spurious rebuilds based on unchanged files which
|
---|
18 | have been regenerated by this task.</p>
|
---|
19 |
|
---|
20 | <p>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
|
---|
21 | type mappers</a> this task needs a supporting regular expression
|
---|
22 | library and an implementation of
|
---|
23 | <code>org.apache.tools.ant.util.regexp.Regexp</code>. See details <a href="#implementation">below</a>. </p>
|
---|
24 |
|
---|
25 | <h3>Parameters</h3>
|
---|
26 | <table border="1" cellpadding="2" cellspacing="0">
|
---|
27 | <tr>
|
---|
28 | <td valign="top"><b>Attribute</b></td>
|
---|
29 | <td valign="top"><b>Description</b></td>
|
---|
30 | <td align="center" valign="top"><b>Required</b></td>
|
---|
31 | </tr>
|
---|
32 | <tr>
|
---|
33 | <td valign="top">file</td>
|
---|
34 | <td valign="top">file for which the regular expression should be replaced.</td>
|
---|
35 | <td align="center">Yes if no nested <fileset> is used</td>
|
---|
36 | </tr>
|
---|
37 | <tr>
|
---|
38 | <td valign="top">match</td>
|
---|
39 | <td valign="top">The regular expression pattern to match in the file(s)</td>
|
---|
40 | <td align="center">Yes, if no nested <regexp> is used</td>
|
---|
41 | </tr>
|
---|
42 | <tr>
|
---|
43 | <td valign="top">replace</td>
|
---|
44 | <td valign="top">The substitution pattern to place in the file(s) in place
|
---|
45 | of the regular expression.</td>
|
---|
46 | <td align="center">Yes, if no nested <substitution> is used</td>
|
---|
47 | </tr>
|
---|
48 | <tr>
|
---|
49 | <td valign="top">flags</td>
|
---|
50 | <td valign="top">The flags to use when matching the regular expression. For more
|
---|
51 | information, consult the Perl5 syntax<br />
|
---|
52 | g : Global replacement. Replace all occurences found<br />
|
---|
53 | i : Case Insensitive. Do not consider case in the match<br />
|
---|
54 | 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.<br />
|
---|
55 | 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.<br />
|
---|
56 | </td>
|
---|
57 | <td valign="top" align="center">No</td>
|
---|
58 | </tr>
|
---|
59 | <tr>
|
---|
60 | <td valign="top">byline</td>
|
---|
61 | <td valign="top">Process the file(s) one line at a time, executing the replacement
|
---|
62 | on one line at a time (<i>true/false</i>). This is useful if you
|
---|
63 | want to only replace the first occurence of a regular expression on
|
---|
64 | each line, which is not easy to do when processing the file as a whole.
|
---|
65 | Defaults to <i>false</i>.</td>
|
---|
66 | <td valign="top" align="center">No</td>
|
---|
67 | </tr>
|
---|
68 | <tr>
|
---|
69 | <td valign="top">encoding</td>
|
---|
70 | <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
|
---|
71 | <td align="center">No - defaults to default JVM encoding</td>
|
---|
72 | </tr>
|
---|
73 | </table>
|
---|
74 | <h3>Examples</h3>
|
---|
75 | <pre> <replaceregexp file="${src}/build.properties"
|
---|
76 | match="OldProperty=(.*)"
|
---|
77 | replace="NewProperty=\1"
|
---|
78 | byline="true"/>
|
---|
79 | </pre>
|
---|
80 | <p>replaces occurrences of the property name "OldProperty"
|
---|
81 | with "NewProperty" in a properties file, preserving the existing
|
---|
82 | value, in the file <code>${src}/build.properties</code></p>
|
---|
83 | <a name="implementation"/>
|
---|
84 | <h3>Choice of regular expression implementation</h3>
|
---|
85 | <p>
|
---|
86 | Ant comes with
|
---|
87 | wrappers for
|
---|
88 | <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/regex/package-summary.html" target="_top">the java.util.regex package of JDK 1.4</a>,
|
---|
89 | <a href="http://jakarta.apache.org/regexp/" target="_top">jakarta-regexp</a>
|
---|
90 | and <a href="http://jakarta.apache.org/oro/" target="_top">jakarta-ORO</a>,
|
---|
91 | See <a href="../install.html#librarydependencies">installation dependencies</a>
|
---|
92 | concerning the supporting libraries.</p>
|
---|
93 | <p>
|
---|
94 | The system property <code>ant.regexp.regexpimpl</code> governs which regular expression implementation will be chosen.
|
---|
95 | Possible values for this property are :
|
---|
96 | <ul>
|
---|
97 | <li>
|
---|
98 | org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
|
---|
99 | </li>
|
---|
100 | <li>
|
---|
101 | org.apache.tools.ant.util.regexp.JakartaOroRegexp
|
---|
102 | </li>
|
---|
103 | <li>
|
---|
104 | org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
|
---|
105 | </li>
|
---|
106 | </ul>
|
---|
107 | It can also be another implementation of the interface <code>org.apache.tools.ant.util.regexp.Regexp</code>.
|
---|
108 | If <code>ant.regexp.regexpimpl</code> is not defined, ant checks in the order Jdk14Regexp, JakartaOroRegexp,
|
---|
109 | JakartaRegexp for the availability of the corresponding library. The first of these 3 which is found will be used.</p>
|
---|
110 | <p>
|
---|
111 | There are cross-platform issues for matches related to line terminator.
|
---|
112 | For example if you use $ to anchor your regular expression on the end of a line
|
---|
113 | the results might be very different depending on both your platform and the regular
|
---|
114 | expression library you use. It is 'highly recommended' that you test your pattern on
|
---|
115 | both Unix and Windows platforms before you rely on it.
|
---|
116 | <ul>
|
---|
117 | <li>Jakarta Oro defines a line terminator as '\n' and is consistent with Perl.</li>
|
---|
118 | <li>Jakarta RegExp uses a system-dependant line terminator.</li>
|
---|
119 | <li>JDK 1.4 uses '\n', '\r\n', '\u0085', '\u2028', '\u2029' as a default
|
---|
120 | but is configured in the wrapper to use only '\n' (UNIX_LINE)</li>
|
---|
121 | </ul>
|
---|
122 | <em>We <b>strongly</b> recommend that you use Jakarta Oro.</em>
|
---|
123 | </p>
|
---|
124 | <h3>Parameters specified as nested elements</h3>
|
---|
125 | <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
|
---|
126 | element.</p>
|
---|
127 | <p>This task supports a nested <i>Regexp</i> element to specify
|
---|
128 | the regular expression. You can use this element to refer to a previously
|
---|
129 | defined regular expression datatype instance.</p>
|
---|
130 | <blockquote>
|
---|
131 | <regexp id="id" pattern="alpha(.+)beta"/><br />
|
---|
132 | <regexp refid="id"/>
|
---|
133 | </blockquote>
|
---|
134 | <p>This task supports a nested <i>Substitution</i> element to specify
|
---|
135 | the substitution pattern. You can use this element to refer to a previously
|
---|
136 | defined substitution pattern datatype instance.</p>
|
---|
137 | <blockquote>
|
---|
138 | <substitution id="id" expression="beta\1alpha"/><br />
|
---|
139 | <substitution refid="id"/>
|
---|
140 | </blockquote>
|
---|
141 | <h3>Examples</h3>
|
---|
142 | <blockquote>
|
---|
143 | <pre>
|
---|
144 | <replaceregexp byline="true">
|
---|
145 | <regexp pattern="OldProperty=(.*)"/>
|
---|
146 | <substitution expression="NewProperty=\1"/>
|
---|
147 | <fileset dir=".">
|
---|
148 | <includes="*.properties"/>
|
---|
149 | </fileset>
|
---|
150 | </replaceregexp>
|
---|
151 | </pre></blockquote>
|
---|
152 | <p>replaces occurrences of the property name "OldProperty"
|
---|
153 | with "NewProperty" in a properties file, preserving the existing
|
---|
154 | value, in all files ending in <code>.properties</code> in the current directory</p>
|
---|
155 |
|
---|
156 | <blockquote>
|
---|
157 | <pre><replaceregexp match="\s+" replace=" " flags="g" byline="true">
|
---|
158 | <fileset dir="${html.dir}" includes="**/*.html" />
|
---|
159 | </replaceregexp>
|
---|
160 | </pre></blockquote>
|
---|
161 | <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
|
---|
162 | line separator. So with input
|
---|
163 |
|
---|
164 | <blockquote>
|
---|
165 | <pre>
|
---|
166 | <html> <body>
|
---|
167 | <<TAB>><h1> T E S T </h1> <<TAB>>
|
---|
168 | <<TAB>> </body></html>
|
---|
169 | </pre></blockquote>
|
---|
170 | would converted to
|
---|
171 | <pre>
|
---|
172 | <html> <body>
|
---|
173 | <h1> T E S T </h1> </body></html>
|
---|
174 | </pre>
|
---|
175 | </p>
|
---|
176 | <hr/>
|
---|
177 | <p align="center">Copyright © 2001-2004 The Apache Software Foundation. All rights
|
---|
178 | Reserved.</p>
|
---|
179 |
|
---|
180 | </body>
|
---|
181 | </html>
|
---|
182 |
|
---|