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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>ReplaceRegExp Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="replace">ReplaceRegExp</a></h2>
  8. <h3>Description</h3>
  9. <p>ReplaceRegExp is a directory based task for replacing the
  10. occurrence of a given regular expression with a substitution pattern
  11. in a selected file or set of files.</p>
  12. <p>The output file is only written if it differs from the existing
  13. file. This prevents spurious rebuilds based on unchanged files which
  14. have been regenerated by this task.</p>
  15. <p>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
  16. type mappers</a> this task needs a supporting regular expression
  17. library and an implementation of
  18. <code>org.apache.tools.ant.util.regexp.Regexp</code>. Ant comes with
  19. implementations for
  20. <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/regex/package-summary.html" target="_top">the java.util.regex package of JDK 1.4</a>,
  21. <a href="http://jakarta.apache.org/regexp/" target="_top">jakarta-regexp</a>
  22. and <a href="http://jakarta.apache.org/oro/" target="_top">jakarta-ORO</a>,
  23. but you will still need the library itself.</p>
  24. <p>
  25. <i>
  26. There are cross-platform issues for matches related to line terminator.
  27. For example if you use $ to anchor your regular expression on the end of a line
  28. the results might be very different depending on both your platform and the regular
  29. expression library you use. It is 'highly recommended' that you test your pattern on
  30. both Unix and Windows platforms before you rely on it.
  31. <ul>
  32. <li>Jakarta Oro defines a line terminator as '\n' and is consistent with Perl.</li>
  33. <li>Jakarta RegExp uses a system-dependant line terminator.</li>
  34. <li>JDK 1.4 uses '\n', '\r\n', '\u0085', '\u2028', '\u2029' as a default
  35. but is configured in the wrapper to use only '\n' (UNIX_LINE)</li>
  36. </ul>
  37. We <b>strongly</b> recommend that you use Jakarta Oro.
  38. </i>
  39. </p>
  40. <h3>Parameters</h3>
  41. <table border="1" cellpadding="2" cellspacing="0">
  42. <tr>
  43. <td valign="top"><b>Attribute</b></td>
  44. <td valign="top"><b>Description</b></td>
  45. <td align="center" valign="top"><b>Required</b></td>
  46. </tr>
  47. <tr>
  48. <td valign="top">file</td>
  49. <td valign="top">file for which the regular expression should be replaced.</td>
  50. <td align="center">Yes if no nested &lt;fileset&gt; is used</td>
  51. </tr>
  52. <tr>
  53. <td valign="top">match</td>
  54. <td valign="top">The regular expression pattern to match in the file(s)</td>
  55. <td align="center">Yes, if no nested &lt;regexp&gt; is used</td>
  56. </tr>
  57. <tr>
  58. <td valign="top">replace</td>
  59. <td valign="top">The substitution pattern to place in the file(s) in place
  60. of the regular expression.</td>
  61. <td align="center">Yes, if no nested &lt;substitution&gt; is used</td>
  62. </tr>
  63. <tr>
  64. <td valign="top">flags</td>
  65. <td valign="top">The flags to use when matching the regular expression. For more
  66. information, consult the Perl5 syntax<br />
  67. g : Global replacement. Replace all occurences found<br />
  68. i : Case Insensitive. Do not consider case in the match<br />
  69. 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 />
  70. 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 />
  71. <td valign="top" align="center">No</td>
  72. </tr>
  73. <tr>
  74. <td valign="top">byline</td>
  75. <td valign="top">Process the file(s) one line at a time, executing the replacement
  76. on one line at a time (<i>true/false</i>). This is useful if you
  77. want to only replace the first occurence of a regular expression on
  78. each line, which is not easy to do when processing the file as a whole.
  79. Defaults to <i>false</i>.</td>
  80. <td valign="top" align="center">No</td>
  81. </tr>
  82. <tr>
  83. <td valign="top">encoding</td>
  84. <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
  85. <td align="center">No - defaults to default JVM encoding</td>
  86. </tr>
  87. </table>
  88. <h3>Examples</h3>
  89. <pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  90. match=&quot;OldProperty=(.*)&quot;
  91. replace=&quot;NewProperty=\1&quot;
  92. byline=&quot;true&quot;/&gt;
  93. </pre>
  94. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  95. with &quot;NewProperty&quot; in a properties file, preserving the existing
  96. value, in the file <code>${src}/build.properties</code></p>
  97. <h3>Parameters specified as nested elements</h3>
  98. <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
  99. element.</p>
  100. <p>This task supports a nested <i>Regexp</i> element to specify
  101. the regular expression. You can use this element to refer to a previously
  102. defined regular expression datatype instance.</p>
  103. <blockquote>
  104. &lt;regexp id="id" pattern="expression"/&gt;<br />
  105. &lt;regexp refid="id"/&gt;
  106. </blockquote>
  107. <p>This task supports a nested <i>Substitution</i> element to specify
  108. the substitution pattern. You can use this element to refer to a previously
  109. defined substitution pattern datatype instance.</p>
  110. <blockquote>
  111. &lt;substitution id="id" pattern="expression"/&gt;<br />
  112. &lt;substitution refid="id"/&gt;
  113. </blockquote>
  114. <h3>Examples</h3>
  115. <blockquote>
  116. <pre>
  117. &lt;replaceregexp byline=&quot;true&quot;&gt;
  118. &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
  119. &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
  120. &lt;fileset dir=&quot;.&quot;&gt;
  121. &lt;includes=&quot;*.properties&quot;/&gt;
  122. &lt;/fileset&gt;
  123. &lt;/replaceregexp&gt;
  124. </pre></blockquote>
  125. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  126. with &quot;NewProperty&quot; in a properties file, preserving the existing
  127. value, in all files ending in <code>.properties</code> in the current directory</p>
  128. <blockquote>
  129. <pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
  130. &lt;fileset dir="${html.dir}" includes="**/*.html" /&gt;
  131. &lt;/replaceregexp&gt;
  132. </pre></blockquote>
  133. <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
  134. line separator. So with input
  135. <blockquote>
  136. <pre>
  137. &lt;html> &lt;body&gt;
  138. &lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
  139. &lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
  140. </pre></blockquote>
  141. would converted to
  142. <pre>
  143. &lt;html&gt; &lt;body&gt;
  144. &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
  145. </pre>
  146. <hr>
  147. <p align="center">Copyright &copy; 2001-2003 Apache Software Foundation. All rights
  148. Reserved.</p>
  149. </body>
  150. </html>