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 8.0 kB

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