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
|
---|
29 | occurrence of a given regular expression with a substitution pattern
|
---|
30 | in a selected file or set of files.</p>
|
---|
31 |
|
---|
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>
|
---|
35 |
|
---|
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>
|
---|
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><fileset></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><regexp></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><substitution></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> <replaceregexp file="${src}/build.properties"
|
---|
93 | match="OldProperty=(.*)"
|
---|
94 | replace="NewProperty=\1"
|
---|
95 | byline="true"/>
|
---|
96 | </pre>
|
---|
97 | <p>replaces occurrences of the property name "OldProperty"
|
---|
98 | with "NewProperty" in a properties file, preserving the existing
|
---|
99 | value, 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 | <regexp id="id" pattern="alpha(.+)beta"/><br>
|
---|
109 | <regexp refid="id"/>
|
---|
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 | <substitution id="id" expression="beta\1alpha"/><br>
|
---|
116 | <substitution refid="id"/>
|
---|
117 | </blockquote>
|
---|
118 | <h3>Examples</h3>
|
---|
119 | <blockquote>
|
---|
120 | <pre>
|
---|
121 | <replaceregexp byline="true">
|
---|
122 | <regexp pattern="OldProperty=(.*)"/>
|
---|
123 | <substitution expression="NewProperty=\1"/>
|
---|
124 | <fileset dir=".">
|
---|
125 | <includes="*.properties"/>
|
---|
126 | </fileset>
|
---|
127 | </replaceregexp>
|
---|
128 | </pre></blockquote>
|
---|
129 | <p>replaces occurrences of the property name "OldProperty"
|
---|
130 | with "NewProperty" in a properties file, preserving the existing
|
---|
131 | value, in all files ending in <code>.properties</code> in the current directory</p>
|
---|
132 |
|
---|
133 | <blockquote>
|
---|
134 | <pre><replaceregexp match="\s+" replace=" " flags="g" byline="true">
|
---|
135 | <fileset dir="${html.dir}" includes="**/*.html"/>
|
---|
136 | </replaceregexp>
|
---|
137 | </pre></blockquote>
|
---|
138 | <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
|
---|
139 | line separator. So with input
|
---|
140 |
|
---|
141 | <blockquote>
|
---|
142 | <pre>
|
---|
143 | <html> <body>
|
---|
144 | <<TAB>><h1> T E S T </h1> <<TAB>>
|
---|
145 | <<TAB>> </body></html>
|
---|
146 | </pre></blockquote>
|
---|
147 | would converted to
|
---|
148 | <pre>
|
---|
149 | <html> <body>
|
---|
150 | <h1> T E S T </h1> </body></html>
|
---|
151 | </pre>
|
---|
152 | </p>
|
---|
153 |
|
---|
154 |
|
---|
155 | </body>
|
---|
156 | </html>
|
---|
157 |
|
---|