source: release-kits/lirk3/bin/ant-installer/web/manual1.7.0/manual/OptionalTasks/replaceregexp.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 6.5 KB
Line 
1<!--
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
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
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.
16-->
17<html>
18
19<head>
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>
23</head>
24<body>
25
26<h2><a name="replaceregexp">ReplaceRegExp</a></h2>
27<h3>Description</h3>
28<p>ReplaceRegExp is a directory based task for replacing the
29occurrence of a given regular expression with a substitution pattern
30in a selected file or set of files.</p>
31
32<p>The output file is only written if it differs from the existing
33file. This prevents spurious rebuilds based on unchanged files which
34have been regenerated by this task.</p>
35
36<p>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
37type mappers</a> this task needs a supporting regular expression
38library and an implementation of
39<code>org.apache.tools.ant.util.regexp.Regexp</code>.
40See details in the documentation of the <a href="../CoreTypes/regexp.html#implementation">Regexp Type</a>. </p>
41
42<h3>Parameters</h3>
43<table border="1" cellpadding="2" cellspacing="0">
44 <tr>
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>
48 </tr>
49 <tr>
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>&lt;fileset&gt;</code> is used</td>
53 </tr>
54 <tr>
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>&lt;regexp&gt;</code> is used</td>
58 </tr>
59 <tr>
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>&lt;substitution&gt;</code> is used</td>
64 </tr>
65 <tr>
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>
73 </td>
74 <td valign="top" align="center">No</td>
75 </tr>
76 <tr>
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>
84 </tr>
85 <tr>
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>
89 </tr>
90</table>
91<h3>Examples</h3>
92<pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
93 match=&quot;OldProperty=(.*)&quot;
94 replace=&quot;NewProperty=\1&quot;
95 byline=&quot;true&quot;/&gt;
96</pre>
97<p>replaces occurrences of the property name &quot;OldProperty&quot;
98 with &quot;NewProperty&quot; in a properties file, preserving the existing
99value, in the file <code>${src}/build.properties</code></p>
100
101<h3>Parameters specified as nested elements</h3>
102<p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
103 element.</p>
104<p>This task supports a nested <i><a href="../CoreTypes/regexp.html">Regexp</a></i> element to specify
105 the regular expression. You can use this element to refer to a previously
106 defined regular expression datatype instance.</p>
107<blockquote>
108 &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br>
109 &lt;regexp refid="id"/&gt;
110</blockquote>
111<p>This task supports a nested <i>Substitution</i> element to specify
112 the substitution pattern. You can use this element to refer to a previously
113 defined substitution pattern datatype instance.</p>
114<blockquote>
115 &lt;substitution id="id" expression="beta\1alpha"/&gt;<br>
116 &lt;substitution refid="id"/&gt;
117</blockquote>
118<h3>Examples</h3>
119<blockquote>
120 <pre>
121&lt;replaceregexp byline=&quot;true&quot;&gt;
122 &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
123 &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
124 &lt;fileset dir=&quot;.&quot;&gt;
125 &lt;includes=&quot;*.properties&quot;/&gt;
126 &lt;/fileset&gt;
127 &lt;/replaceregexp&gt;
128</pre></blockquote>
129<p>replaces occurrences of the property name &quot;OldProperty&quot;
130 with &quot;NewProperty&quot; in a properties file, preserving the existing
131value, in all files ending in <code>.properties</code> in the current directory</p>
132
133<blockquote>
134<pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
135 &lt;fileset dir="${html.dir}" includes="**/*.html"/&gt;
136&lt;/replaceregexp&gt;
137</pre></blockquote>
138<p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
139line separator. So with input
140
141<blockquote>
142<pre>
143&lt;html&gt; &lt;body&gt;
144&lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
145&lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
146</pre></blockquote>
147would converted to
148<pre>
149&lt;html&gt; &lt;body&gt;
150 &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
151</pre>
152</p>
153
154
155</body>
156</html>
157
Note: See TracBrowser for help on using the repository browser.