2
Licensed to the Apache Software Foundation (ASF) under one or more
3
contributor license agreements. See the NOTICE file distributed with
4
this work for additional information regarding copyright ownership.
5
The ASF licenses this file to You under the Apache License, Version 2.0
6
(the "License"); you may not use this file except in compliance with
7
the License. You may obtain a copy of the License at
9
http://www.apache.org/licenses/LICENSE-2.0
11
Unless required by applicable law or agreed to in writing, software
12
distributed under the License is distributed on an "AS IS" BASIS,
13
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
See the License for the specific language governing permissions and
15
limitations under the License.
20
<meta http-equiv="Content-Language" content="en-us">
21
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
22
<title>ReplaceRegExp Task</title>
26
<h2><a name="replaceregexp">ReplaceRegExp</a></h2>
28
<p>ReplaceRegExp is a directory based task for replacing the
29
occurrence of a given regular expression with a substitution pattern
30
in a selected file or set of files.</p>
32
<p>The output file is only written if it differs from the existing
33
file. This prevents spurious rebuilds based on unchanged files which
34
have been regenerated by this task.</p>
36
<p>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
37
type mappers</a> this task needs a supporting regular expression
38
library and an implementation of
39
<code>org.apache.tools.ant.util.regexp.Regexp</code>.
40
See details in the documentation of the <a href="../CoreTypes/regexp.html#implementation">Regexp Type</a>. </p>
43
<table border="1" cellpadding="2" cellspacing="0">
45
<td valign="top"><b>Attribute</b></td>
46
<td valign="top"><b>Description</b></td>
47
<td align="center" valign="top"><b>Required</b></td>
50
<td valign="top">file</td>
51
<td valign="top">file for which the regular expression should be replaced.</td>
52
<td align="center">Yes if no nested <code><fileset></code> is used</td>
55
<td valign="top">match</td>
56
<td valign="top">The regular expression pattern to match in the file(s)</td>
57
<td align="center">Yes, if no nested <code><regexp></code> is used</td>
60
<td valign="top">replace</td>
61
<td valign="top">The substitution pattern to place in the file(s) in place
62
of the regular expression.</td>
63
<td align="center">Yes, if no nested <code><substitution></code> is used</td>
66
<td valign="top">flags</td>
67
<td valign="top">The flags to use when matching the regular expression. For more
68
information, consult the Perl5 syntax<br>
69
g : Global replacement. Replace all occurrences found<br>
70
i : Case Insensitive. Do not consider case in the match<br>
71
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>
72
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>
74
<td valign="top" align="center">No</td>
77
<td valign="top">byline</td>
78
<td valign="top">Process the file(s) one line at a time, executing the replacement
79
on one line at a time (<i>true/false</i>). This is useful if you
80
want to only replace the first occurrence of a regular expression on
81
each line, which is not easy to do when processing the file as a whole.
82
Defaults to <i>false</i>.</td>
83
<td valign="top" align="center">No</td>
86
<td valign="top">encoding</td>
87
<td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
88
<td align="center">No - defaults to default JVM encoding</td>
91
<td valign="top">preserveLastModified</td>
92
<td valign="top">Keep the file timestamp(s) even if the file(s)
93
is(are) modified. <em>since Ant 1.8.0.</em></td>
94
<td valign="top" align="center">No, defaults to false</td>
99
<replaceregexp file="${src}/build.properties"
100
match="OldProperty=(.*)"
101
replace="NewProperty=\1"
102
byline="true"
105
<p>replaces occurrences of the property name "OldProperty"
106
with "NewProperty" in a properties file, preserving the existing
107
value, in the file <code>${src}/build.properties</code></p>
109
<h3>Parameters specified as nested elements</h3>
110
<p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
112
<p>Since Ant 1.8.0 this task supports any filesystem
113
based <a href="../CoreTypes/resources.html#collection">resource
114
collections</a> as nested elements.</p>
115
<p>This task supports a nested <i><a href="../CoreTypes/regexp.html">Regexp</a></i> element to specify
116
the regular expression. You can use this element to refer to a previously
117
defined regular expression datatype instance.</p>
119
<regexp id="id" pattern="alpha(.+)beta"/><br>
120
<regexp refid="id"/>
122
<p>This task supports a nested <i>Substitution</i> element to specify
123
the substitution pattern. You can use this element to refer to a previously
124
defined substitution pattern datatype instance.</p>
126
<substitution id="id" expression="beta\1alpha"/><br>
127
<substitution refid="id"/>
132
<replaceregexp byline="true">
133
<regexp pattern="OldProperty=(.*)"/>
134
<substitution expression="NewProperty=\1"/>
135
<fileset dir=".">
136
<include name="*.properties"/>
138
</replaceregexp>
140
<p>replaces occurrences of the property name "OldProperty"
141
with "NewProperty" in a properties file, preserving the existing
142
value, in all files ending in <code>.properties</code> in the current directory</p>
145
<pre><replaceregexp match="\s+" replace=" " flags="g" byline="true">
146
<fileset dir="${html.dir}" includes="**/*.html"/>
147
</replaceregexp>
149
<p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
150
line separator. So with input
154
<html> <body>
155
<<TAB>><h1> T E S T </h1> <<TAB>>
156
<<TAB>> </body></html>
160
<html> <body>
161
<h1> T E S T </h1> </body></html>