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 6.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.</p>
  30. <p>Similar to <a href="../CoreTypes/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="../CoreTypes/regexp.html#implementation">Regexp Type</a>. </p>
  35. <h3>Parameters</h3>
  36. <table border="1" cellpadding="2" cellspacing="0">
  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 Ant 1.6</em></td>
  81. <td align="center">No - defaults to default JVM encoding</td>
  82. </tr>
  83. </table>
  84. <h3>Examples</h3>
  85. <pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  86. match=&quot;OldProperty=(.*)&quot;
  87. replace=&quot;NewProperty=\1&quot;
  88. byline=&quot;true&quot;/&gt;
  89. </pre>
  90. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  91. with &quot;NewProperty&quot; in a properties file, preserving the existing
  92. value, in the file <code>${src}/build.properties</code></p>
  93. <h3>Parameters specified as nested elements</h3>
  94. <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
  95. element.</p>
  96. <p>This task supports a nested <i><a href="../CoreTypes/regexp.html">Regexp</a></i> element to specify
  97. the regular expression. You can use this element to refer to a previously
  98. defined regular expression datatype instance.</p>
  99. <blockquote>
  100. &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br>
  101. &lt;regexp refid="id"/&gt;
  102. </blockquote>
  103. <p>This task supports a nested <i>Substitution</i> element to specify
  104. the substitution pattern. You can use this element to refer to a previously
  105. defined substitution pattern datatype instance.</p>
  106. <blockquote>
  107. &lt;substitution id="id" expression="beta\1alpha"/&gt;<br>
  108. &lt;substitution refid="id"/&gt;
  109. </blockquote>
  110. <h3>Examples</h3>
  111. <blockquote>
  112. <pre>
  113. &lt;replaceregexp byline=&quot;true&quot;&gt;
  114. &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
  115. &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
  116. &lt;fileset dir=&quot;.&quot;&gt;
  117. &lt;includes=&quot;*.properties&quot;/&gt;
  118. &lt;/fileset&gt;
  119. &lt;/replaceregexp&gt;
  120. </pre></blockquote>
  121. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  122. with &quot;NewProperty&quot; in a properties file, preserving the existing
  123. value, in all files ending in <code>.properties</code> in the current directory</p>
  124. <blockquote>
  125. <pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
  126. &lt;fileset dir="${html.dir}" includes="**/*.html"/&gt;
  127. &lt;/replaceregexp&gt;
  128. </pre></blockquote>
  129. <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
  130. line separator. So with input
  131. <blockquote>
  132. <pre>
  133. &lt;html&gt; &lt;body&gt;
  134. &lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
  135. &lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
  136. </pre></blockquote>
  137. would converted to
  138. <pre>
  139. &lt;html&gt; &lt;body&gt;
  140. &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
  141. </pre>
  142. </p>
  143. </body>
  144. </html>