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.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  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>ReplaceRegExp is a directory based task for replacing the
  25. occurrence of a given regular expression with a substitution pattern
  26. in a selected file or set of files.</p>
  27. <p>The output file is only written if it differs from the existing
  28. file. This prevents spurious rebuilds based on unchanged files which
  29. have been regenerated by this task.</p>
  30. <p>Similar to <a href="../Types/mapper.html#regexp-mapper">regexp
  31. type mappers</a> this task needs a supporting regular expression
  32. library and an implementation of
  33. <code>org.apache.tools.ant.util.regexp.Regexp</code>.
  34. See details in the documentation of the <a href="../Types/regexp.html#implementation">Regexp Type</a>.</p>
  35. <h3>Parameters</h3>
  36. <table>
  37. <tr>
  38. <td valign="top"><b>Attribute</b></td>
  39. <td valign="top"><b>Description</b></td>
  40. <td align="center" valign="top"><b>Required</b></td>
  41. </tr>
  42. <tr>
  43. <td valign="top">file</td>
  44. <td valign="top">file for which the regular expression should be replaced.</td>
  45. <td align="center">Yes if no nested <code>&lt;fileset&gt;</code> is used</td>
  46. </tr>
  47. <tr>
  48. <td valign="top">match</td>
  49. <td valign="top">The regular expression pattern to match in the file(s)</td>
  50. <td align="center">Yes, if no nested <code>&lt;regexp&gt;</code> is used</td>
  51. </tr>
  52. <tr>
  53. <td valign="top">replace</td>
  54. <td valign="top">The substitution pattern to place in the file(s) in place
  55. of the regular expression.</td>
  56. <td align="center">Yes, if no nested <code>&lt;substitution&gt;</code> is used</td>
  57. </tr>
  58. <tr>
  59. <td valign="top">flags</td>
  60. <td valign="top">The flags to use when matching the regular expression. For more
  61. information, consult the Perl5 syntax<br>
  62. g : Global replacement. Replace all occurrences found<br>
  63. i : Case Insensitive. Do not consider case in the match<br>
  64. 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>
  65. 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>
  66. </td>
  67. <td valign="top" align="center">No</td>
  68. </tr>
  69. <tr>
  70. <td valign="top">byline</td>
  71. <td valign="top">Process the file(s) one line at a time, executing the replacement
  72. on one line at a time (<i>true/false</i>). This is useful if you
  73. want to only replace the first occurrence of a regular expression on
  74. each line, which is not easy to do when processing the file as a whole.
  75. Defaults to <i>false</i>.</td>
  76. <td valign="top" align="center">No</td>
  77. </tr>
  78. <tr>
  79. <td valign="top">encoding</td>
  80. <td valign="top">The encoding of the file. <em>since Apache Ant 1.6</em></td>
  81. <td align="center">No - defaults to default JVM encoding</td>
  82. </tr>
  83. <tr>
  84. <td valign="top">preserveLastModified</td>
  85. <td valign="top">Keep the file timestamp(s) even if the file(s)
  86. is(are) modified. <em>since Ant 1.8.0</em>.</td>
  87. <td valign="top" align="center">No, defaults to false</td>
  88. </tr>
  89. </table>
  90. <h3>Examples</h3>
  91. <pre>
  92. &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  93. match=&quot;OldProperty=(.*)&quot;
  94. replace=&quot;NewProperty=\1&quot;
  95. byline=&quot;true&quot;
  96. /&gt;
  97. </pre>
  98. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  99. with &quot;NewProperty&quot; in a properties file, preserving the existing
  100. value, in the file <code>${src}/build.properties</code></p>
  101. <h3>Parameters specified as nested elements</h3>
  102. <p>This task supports a nested <a href="../Types/fileset.html">FileSet</a>
  103. element.</p>
  104. <p><em>Since Ant 1.8.0</em>, this task supports any filesystem
  105. based <a href="../Types/resources.html#collection">resource
  106. collections</a> as nested elements.</p>
  107. <p>This task supports a nested <i><a href="../Types/regexp.html">Regexp</a></i> element to specify
  108. the regular expression. You can use this element to refer to a previously
  109. defined regular expression datatype instance.</p>
  110. <blockquote>
  111. &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br>
  112. &lt;regexp refid="id"/&gt;
  113. </blockquote>
  114. <p>This task supports a nested <i>Substitution</i> element to specify
  115. the substitution pattern. You can use this element to refer to a previously
  116. defined substitution pattern datatype instance.</p>
  117. <blockquote>
  118. &lt;substitution id="id" expression="beta\1alpha"/&gt;<br>
  119. &lt;substitution refid="id"/&gt;
  120. </blockquote>
  121. <h3>Examples</h3>
  122. <blockquote>
  123. <pre>
  124. &lt;replaceregexp byline=&quot;true&quot;&gt;
  125. &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
  126. &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
  127. &lt;fileset dir=&quot;.&quot;&gt;
  128. &lt;include name=&quot;*.properties&quot;/&gt;
  129. &lt;/fileset&gt;
  130. &lt;/replaceregexp&gt;
  131. </pre></blockquote>
  132. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  133. with &quot;NewProperty&quot; in a properties file, preserving the existing
  134. value, in all files ending in <code>.properties</code> in the current directory</p>
  135. <br>
  136. <blockquote>
  137. <pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
  138. &lt;fileset dir="${html.dir}" includes="**/*.html"/&gt;
  139. &lt;/replaceregexp&gt;
  140. </pre></blockquote>
  141. <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
  142. line separator. So with input</p>
  143. <blockquote>
  144. <pre>
  145. &lt;html&gt; &lt;body&gt;
  146. &lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
  147. &lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
  148. </pre></blockquote>
  149. <p>would converted to</p>
  150. <blockquote>
  151. <pre>
  152. &lt;html&gt; &lt;body&gt;
  153. &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
  154. </pre>
  155. </blockquote>
  156. <br><!-- small distance from code of the previous example -->
  157. <blockquote>
  158. <pre>&lt;replaceregexp match="\\n" replace="${line.separator}" flags="g" byline="true"&gt;
  159. &lt;fileset dir="${dir}"/&gt;
  160. &lt;/replaceregexp&gt;
  161. </pre></blockquote>
  162. <p>replaces all <tt>\n</tt> markers (beware the quoting of the backslash) by a line break.
  163. So with input</p>
  164. <blockquote>
  165. <pre>
  166. one\ntwo\nthree
  167. </pre></blockquote>
  168. <p>would converted to</p>
  169. <blockquote>
  170. <pre>
  171. one
  172. two
  173. three
  174. </pre>
  175. </blockquote>
  176. <p>Beware that inserting line breaks could break file syntax. For example in xml:</p>
  177. <blockquote>
  178. <pre>
  179. &lt;root&gt;
  180. &lt;text&gt;line breaks \n should work in text&lt;/text&gt;
  181. &lt;attribute value=&quot;but breaks \n attributes&quot; /&gt;
  182. &lt;/root&gt;
  183. </pre>
  184. </blockquote>
  185. </body>
  186. </html>