You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

replaceregexp.html 7.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>ReplaceRegExp Task</title>
  20. </head>
  21. <body>
  22. <h2 id="replaceregexp">ReplaceRegExp</h2>
  23. <h3>Description</h3>
  24. <p><code>ReplaceRegExp</code> is a directory based task for replacing the occurrence of a given
  25. regular expression with a substitution pattern in a selected file or set of files.</p>
  26. <p>The output file is only written if it differs from the existing
  27. file. This prevents spurious rebuilds based on unchanged files which
  28. have been regenerated by this task. In order to assess whether a file
  29. has changed, this task will create a pre-processed version of the
  30. source file inside of the <a href="../running.html#tmpdir">temporary
  31. directory</a>.</p>
  32. <p>Similar to <a href="../Types/mapper.html#regexp-mapper">regexp type mappers</a> this task needs a
  33. supporting regular expression library and an implementation
  34. of <code class="code">org.apache.tools.ant.util.regexp.Regexp</code>. See details in the
  35. documentation of the <a href="../Types/regexp.html#implementation">Regexp Type</a>.</p>
  36. <h3>Parameters</h3>
  37. <table class="attr">
  38. <tr>
  39. <th scope="col">Attribute</th>
  40. <th scope="col">Description</th>
  41. <th scope="col">Required</th>
  42. </tr>
  43. <tr>
  44. <td>file</td>
  45. <td>file for which the regular expression should be replaced.</td>
  46. <td>Yes, unless nested <code>&lt;fileset&gt;</code> is used</td>
  47. </tr>
  48. <tr>
  49. <td>match</td>
  50. <td>The regular expression pattern to match in the file(s)</td>
  51. <td>Yes, unless nested <code>&lt;regexp&gt;</code> is used</td>
  52. </tr>
  53. <tr>
  54. <td>replace</td>
  55. <td>The substitution pattern to place in the file(s) in place of the regular expression.</td>
  56. <td>Yes, unless nested <code>&lt;substitution&gt;</code> is used</td>
  57. </tr>
  58. <tr>
  59. <td>flags</td>
  60. <td>The flags to use when matching the regular expression. For more information, consult the
  61. Perl 5 syntax<br/>
  62. <q>g</q> : Global replacement. Replace all occurrences found<br/>
  63. <q>i</q> : Case Insensitive. Do not consider case in the match<br/>
  64. <q>m</q> : Multiline. Treat the string as multiple lines of input, using <q>^</q>
  65. and <q>$</q> as the start or end of any line, respectively, rather than start or end of
  66. string.<br/>
  67. <q>s</q> : Singleline. Treat the string as a single line of input, using <q>.</q> to match
  68. any character, including a newline, which normally, it would not match.
  69. </td>
  70. <td>No</td>
  71. </tr>
  72. <tr>
  73. <td>byline</td>
  74. <td>Process the file(s) one line at a time, executing the replacement on one line at a time
  75. (<q>true|false</q>). This is useful if you want to only replace the first occurrence of a
  76. regular expression on each line, which is not easy to do when processing the file as a
  77. whole.</td>
  78. <td>No; defaults to <q>false</q></td>
  79. </tr>
  80. <tr>
  81. <td>encoding</td>
  82. <td>The encoding of the file. <em>since Apache Ant 1.6</em></td>
  83. <td>No; defaults to default JVM character encoding</td>
  84. </tr>
  85. <tr>
  86. <td>preserveLastModified</td>
  87. <td>Keep the file timestamp(s) even if the file(s) is(are) modified. <em>since Ant
  88. 1.8.0</em>.</td>
  89. <td>No; defaults to <q>false</q></td>
  90. </tr>
  91. </table>
  92. <h3>Examples</h3>
  93. <pre>
  94. &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  95. match=&quot;OldProperty=(.*)&quot;
  96. replace=&quot;NewProperty=\1&quot;
  97. byline=&quot;true&quot;/&gt;</pre>
  98. <p>replaces occurrences of the property name <q>OldProperty</q> with <q>NewProperty</q> in a
  99. properties file, preserving the existing value, in the file <samp>${src}/build.properties</samp></p>
  100. <h3>Parameters specified as nested elements</h3>
  101. <p>This task supports a nested <a href="../Types/fileset.html">FileSet</a> element.</p>
  102. <p><em>Since Ant 1.8.0</em>, this task supports any filesystem
  103. based <a href="../Types/resources.html#collection">resource collections</a> as nested elements.</p>
  104. <p>This task supports a nested <a href="../Types/regexp.html">Regexp</a> element to specify the
  105. regular expression. You can use this element to refer to a previously defined regular expression
  106. datatype instance.</p>
  107. <pre>
  108. &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;
  109. &lt;regexp refid="id"/&gt;</pre>
  110. <p>This task supports a nested <code>substitution</code> element to specify the substitution
  111. pattern. You can use this element to refer to a previously defined substitution pattern datatype
  112. instance.</p>
  113. <pre>
  114. &lt;substitution id="id" expression="beta\1alpha"/&gt;<br/>
  115. &lt;substitution refid="id"/&gt;</pre>
  116. <h3>Examples</h3>
  117. <p>Replace occurrences of the property name <q>OldProperty</q> with <q>NewProperty</q> in a
  118. properties file, preserving the existing value, in all files ending in <samp>.properties</samp> in
  119. the current directory:</p>
  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;include name=&quot;*.properties&quot;/&gt;
  126. &lt;/fileset&gt;
  127. &lt;/replaceregexp&gt;</pre>
  128. <p>Replace all whitespaces (blanks, tabs, etc) by one blank remaining the line separator:</p>
  129. <pre>
  130. &lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
  131. &lt;fileset dir="${html.dir}" includes="**/*.html"/&gt;
  132. &lt;/replaceregexp&gt;</pre>
  133. <p>Then, input</p>
  134. <pre>
  135. &lt;html&gt; &lt;body&gt;
  136. &lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
  137. &lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;</pre>
  138. <p>is converted to</p>
  139. <pre>
  140. &lt;html&gt; &lt;body&gt;
  141. &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;</pre>
  142. <p>The task</p>
  143. <pre>
  144. &lt;replaceregexp match="\\n" replace="${line.separator}" flags="g" byline="true"&gt;
  145. &lt;fileset dir="${dir}"/&gt;
  146. &lt;/replaceregexp&gt;</pre>
  147. <p>replaces all <q>\n</q> markers (beware the quoting of the backslash) by a line break. Then,
  148. input</p>
  149. <pre>one\ntwo\nthree</pre>
  150. <p>is converted to</p>
  151. <pre>
  152. one
  153. two
  154. three</pre>
  155. <p>Beware that inserting line breaks could break file syntax. For example in XML:</p>
  156. <pre>
  157. &lt;root&gt;
  158. &lt;text&gt;line breaks \n should work in text&lt;/text&gt;
  159. &lt;attribute value=&quot;but breaks \n attributes&quot;/&gt;
  160. &lt;/root&gt;</pre>
  161. </body>
  162. </html>