2018-12-21 11:20:43 +01:00
|
|
|
<!DOCTYPE html>
|
2006-09-11 04:19:00 +00:00
|
|
|
<!--
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
2019-05-25 13:41:47 +02:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2006-09-11 04:19:00 +00:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
-->
|
2018-12-21 11:20:43 +01:00
|
|
|
<html lang="en">
|
2001-11-12 11:26:48 +00:00
|
|
|
|
|
|
|
<head>
|
2005-04-29 18:58:16 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
|
2003-06-24 16:07:32 +00:00
|
|
|
<title>ReplaceRegExp Task</title>
|
2001-11-12 11:26:48 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2018-01-22 23:52:21 +01:00
|
|
|
<h2 id="replaceregexp">ReplaceRegExp</h2>
|
2001-11-12 11:26:48 +00:00
|
|
|
<h3>Description</h3>
|
2018-02-28 07:58:59 +01:00
|
|
|
<p><code>ReplaceRegExp</code> 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.</p>
|
2001-11-12 11:26:48 +00:00
|
|
|
|
2003-07-23 12:57:18 +00:00
|
|
|
<p>The output file is only written if it differs from the existing
|
|
|
|
file. This prevents spurious rebuilds based on unchanged files which
|
2020-03-08 17:14:44 +01:00
|
|
|
have been regenerated by this task. In order to assess whether a file
|
|
|
|
has changed, this task will create a pre-processed version of the
|
|
|
|
source file inside of the <a href="../running.html#tmpdir">temporary
|
|
|
|
directory</a>.</p>
|
2003-07-23 12:57:18 +00:00
|
|
|
|
2018-02-28 07:58:59 +01:00
|
|
|
<p>Similar to <a href="../Types/mapper.html#regexp-mapper">regexp type mappers</a> this task needs a
|
|
|
|
supporting regular expression library and an implementation
|
2018-03-10 20:17:33 +01:00
|
|
|
of <code class="code">org.apache.tools.ant.util.regexp.Regexp</code>. See details in the
|
|
|
|
documentation of the <a href="../Types/regexp.html#implementation">Regexp Type</a>.</p>
|
2001-11-12 11:26:48 +00:00
|
|
|
|
|
|
|
<h3>Parameters</h3>
|
2018-02-28 07:58:59 +01:00
|
|
|
<table class="attr">
|
2001-11-12 11:26:48 +00:00
|
|
|
<tr>
|
2018-05-15 10:29:27 +02:00
|
|
|
<th scope="col">Attribute</th>
|
|
|
|
<th scope="col">Description</th>
|
|
|
|
<th scope="col">Required</th>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>file</td>
|
|
|
|
<td>file for which the regular expression should be replaced.</td>
|
|
|
|
<td>Yes, unless nested <code><fileset></code> is used</td>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>match</td>
|
|
|
|
<td>The regular expression pattern to match in the file(s)</td>
|
|
|
|
<td>Yes, unless nested <code><regexp></code> is used</td>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>replace</td>
|
|
|
|
<td>The substitution pattern to place in the file(s) in place of the regular expression.</td>
|
|
|
|
<td>Yes, unless nested <code><substitution></code> is used</td>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>flags</td>
|
|
|
|
<td>The flags to use when matching the regular expression. For more information, consult the
|
2018-03-02 08:23:45 +01:00
|
|
|
Perl 5 syntax<br/>
|
|
|
|
<q>g</q> : Global replacement. Replace all occurrences found<br/>
|
|
|
|
<q>i</q> : Case Insensitive. Do not consider case in the match<br/>
|
2018-02-28 07:58:59 +01:00
|
|
|
<q>m</q> : Multiline. Treat the string as multiple lines of input, using <q>^</q>
|
|
|
|
and <q>$</q> as the start or end of any line, respectively, rather than start or end of
|
2018-03-02 08:23:45 +01:00
|
|
|
string.<br/>
|
2018-02-28 07:58:59 +01:00
|
|
|
<q>s</q> : Singleline. Treat the string as a single line of input, using <q>.</q> to match
|
2018-03-02 08:23:45 +01:00
|
|
|
any character, including a newline, which normally, it would not match.
|
2003-11-24 21:56:53 +00:00
|
|
|
</td>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>No</td>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>byline</td>
|
|
|
|
<td>Process the file(s) one line at a time, executing the replacement on one line at a time
|
|
|
|
(<q>true|false</q>). This is useful if you want to only replace the first occurrence of a
|
|
|
|
regular expression on each line, which is not easy to do when processing the file as a
|
|
|
|
whole.</td>
|
|
|
|
<td>No; defaults to <q>false</q></td>
|
2001-11-12 11:26:48 +00:00
|
|
|
</tr>
|
2003-01-31 12:15:18 +00:00
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>encoding</td>
|
|
|
|
<td>The encoding of the file. <em>since Apache Ant 1.6</em></td>
|
|
|
|
<td>No; defaults to default JVM character encoding</td>
|
2003-01-31 12:15:18 +00:00
|
|
|
</tr>
|
2008-12-05 16:38:47 +00:00
|
|
|
<tr>
|
2018-02-28 07:58:59 +01:00
|
|
|
<td>preserveLastModified</td>
|
|
|
|
<td>Keep the file timestamp(s) even if the file(s) is(are) modified. <em>since Ant
|
|
|
|
1.8.0</em>.</td>
|
|
|
|
<td>No; defaults to <q>false</q></td>
|
2008-12-05 16:38:47 +00:00
|
|
|
</tr>
|
2023-11-17 06:11:40 +01:00
|
|
|
<tr>
|
|
|
|
<td>failonerror</td>
|
|
|
|
<td>Stop the build process if certain file operations fail.<br/>
|
|
|
|
For historical reasons the task swallows and logs certain
|
|
|
|
failure cases, enabling this option makes sure the exception
|
|
|
|
make the build fail instead.
|
|
|
|
<em>since Ant 1.10.15</em></td>
|
|
|
|
<td>No; defaults to <q>false</q></td>
|
|
|
|
</tr>
|
2001-11-12 11:26:48 +00:00
|
|
|
</table>
|
|
|
|
<h3>Examples</h3>
|
2007-12-12 06:08:22 +00:00
|
|
|
<pre>
|
|
|
|
<replaceregexp file="${src}/build.properties"
|
|
|
|
match="OldProperty=(.*)"
|
|
|
|
replace="NewProperty=\1"
|
2018-02-28 07:58:59 +01:00
|
|
|
byline="true"/></pre>
|
|
|
|
<p>replaces occurrences of the property name <q>OldProperty</q> with <q>NewProperty</q> in a
|
|
|
|
properties file, preserving the existing value, in the file <samp>${src}/build.properties</samp></p>
|
2003-12-01 22:03:25 +00:00
|
|
|
|
2001-11-12 11:26:48 +00:00
|
|
|
<h3>Parameters specified as nested elements</h3>
|
2018-02-28 07:58:59 +01:00
|
|
|
<p>This task supports a nested <a href="../Types/fileset.html">FileSet</a> element.</p>
|
2018-01-25 11:25:28 +01:00
|
|
|
<p><em>Since Ant 1.8.0</em>, this task supports any filesystem
|
2018-02-28 07:58:59 +01:00
|
|
|
based <a href="../Types/resources.html#collection">resource collections</a> as nested elements.</p>
|
|
|
|
<p>This task supports a nested <a href="../Types/regexp.html">Regexp</a> element to specify the
|
|
|
|
regular expression. You can use this element to refer to a previously defined regular expression
|
|
|
|
datatype instance.</p>
|
2018-02-09 06:54:03 +01:00
|
|
|
|
2018-02-28 07:58:59 +01:00
|
|
|
<pre>
|
|
|
|
<regexp id="id" pattern="alpha(.+)beta"/>
|
|
|
|
<regexp refid="id"/></pre>
|
2018-02-09 06:54:03 +01:00
|
|
|
|
2018-03-05 17:35:30 +01:00
|
|
|
<p>This task supports a nested <code>substitution</code> element to specify the substitution
|
2018-02-28 07:58:59 +01:00
|
|
|
pattern. You can use this element to refer to a previously defined substitution pattern datatype
|
|
|
|
instance.</p>
|
2015-06-08 08:42:57 +02:00
|
|
|
|
2018-02-28 07:58:59 +01:00
|
|
|
<pre>
|
2018-03-02 08:23:45 +01:00
|
|
|
<substitution id="id" expression="beta\1alpha"/><br/>
|
2018-02-28 07:58:59 +01:00
|
|
|
<substitution refid="id"/></pre>
|
2015-06-08 08:42:57 +02:00
|
|
|
|
2001-11-12 11:26:48 +00:00
|
|
|
<h3>Examples</h3>
|
2015-06-08 08:42:57 +02:00
|
|
|
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>Replace occurrences of the property name <q>OldProperty</q> with <q>NewProperty</q> in a
|
|
|
|
properties file, preserving the existing value, in all files ending in <samp>.properties</samp> in
|
|
|
|
the current directory:</p>
|
2018-02-09 06:54:03 +01:00
|
|
|
<pre>
|
2001-11-12 11:26:48 +00:00
|
|
|
<replaceregexp byline="true">
|
2018-02-28 07:58:59 +01:00
|
|
|
<regexp pattern="OldProperty=(.*)"/>
|
|
|
|
<substitution expression="NewProperty=\1"/>
|
|
|
|
<fileset dir=".">
|
|
|
|
<include name="*.properties"/>
|
|
|
|
</fileset>
|
|
|
|
</replaceregexp></pre>
|
|
|
|
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>Replace all whitespaces (blanks, tabs, etc) by one blank remaining the line separator:</p>
|
2018-02-28 07:58:59 +01:00
|
|
|
<pre>
|
|
|
|
<replaceregexp match="\s+" replace=" " flags="g" byline="true">
|
2004-11-19 09:07:12 +00:00
|
|
|
<fileset dir="${html.dir}" includes="**/*.html"/>
|
2018-02-28 07:58:59 +01:00
|
|
|
</replaceregexp></pre>
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>Then, input</p>
|
2003-03-19 08:42:14 +00:00
|
|
|
<pre>
|
2004-11-16 08:12:35 +00:00
|
|
|
<html> <body>
|
2018-01-22 23:52:21 +01:00
|
|
|
<<TAB>><h1> T E S T </h1> <<TAB>>
|
2018-02-28 07:58:59 +01:00
|
|
|
<<TAB>> </body></html></pre>
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>is converted to</p>
|
2003-03-19 08:42:14 +00:00
|
|
|
<pre>
|
|
|
|
<html> <body>
|
2018-02-28 07:58:59 +01:00
|
|
|
<h1> T E S T </h1> </body></html></pre>
|
2015-06-08 08:42:57 +02:00
|
|
|
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>The task</p>
|
2015-06-08 08:42:57 +02:00
|
|
|
<pre>
|
2018-02-28 07:58:59 +01:00
|
|
|
<replaceregexp match="\\n" replace="${line.separator}" flags="g" byline="true">
|
|
|
|
<fileset dir="${dir}"/>
|
|
|
|
</replaceregexp></pre>
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>replaces all <q>\n</q> markers (beware the quoting of the backslash) by a line break. Then,
|
2018-02-28 07:58:59 +01:00
|
|
|
input</p>
|
|
|
|
<pre>one\ntwo\nthree</pre>
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>is converted to</p>
|
2015-06-08 08:42:57 +02:00
|
|
|
<pre>
|
|
|
|
one
|
|
|
|
two
|
2018-02-28 07:58:59 +01:00
|
|
|
three</pre>
|
2018-02-09 06:54:03 +01:00
|
|
|
|
2018-05-15 09:51:58 +02:00
|
|
|
<p>Beware that inserting line breaks could break file syntax. For example in XML:</p>
|
2015-06-08 08:42:57 +02:00
|
|
|
<pre>
|
|
|
|
<root>
|
2018-02-28 07:58:59 +01:00
|
|
|
<text>line breaks \n should work in text</text>
|
|
|
|
<attribute value="but breaks \n attributes"/>
|
|
|
|
</root></pre>
|
2015-06-08 08:42:57 +02:00
|
|
|
|
2001-11-12 11:26:48 +00:00
|
|
|
</body>
|
|
|
|
</html>
|