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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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="replaceregexp">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>.
  19. See details in the documentation of the <a href="../CoreTypes/regexp.html#implementation">Regexp Type</a>. </p>
  20. <h3>Parameters</h3>
  21. <table border="1" cellpadding="2" cellspacing="0">
  22. <tr>
  23. <td valign="top"><b>Attribute</b></td>
  24. <td valign="top"><b>Description</b></td>
  25. <td align="center" valign="top"><b>Required</b></td>
  26. </tr>
  27. <tr>
  28. <td valign="top">file</td>
  29. <td valign="top">file for which the regular expression should be replaced.</td>
  30. <td align="center">Yes if no nested &lt;fileset&gt; is used</td>
  31. </tr>
  32. <tr>
  33. <td valign="top">match</td>
  34. <td valign="top">The regular expression pattern to match in the file(s)</td>
  35. <td align="center">Yes, if no nested &lt;regexp&gt; is used</td>
  36. </tr>
  37. <tr>
  38. <td valign="top">replace</td>
  39. <td valign="top">The substitution pattern to place in the file(s) in place
  40. of the regular expression.</td>
  41. <td align="center">Yes, if no nested &lt;substitution&gt; is used</td>
  42. </tr>
  43. <tr>
  44. <td valign="top">flags</td>
  45. <td valign="top">The flags to use when matching the regular expression. For more
  46. information, consult the Perl5 syntax<br />
  47. g : Global replacement. Replace all occurences found<br />
  48. i : Case Insensitive. Do not consider case in the match<br />
  49. 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 />
  50. 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 />
  51. </td>
  52. <td valign="top" align="center">No</td>
  53. </tr>
  54. <tr>
  55. <td valign="top">byline</td>
  56. <td valign="top">Process the file(s) one line at a time, executing the replacement
  57. on one line at a time (<i>true/false</i>). This is useful if you
  58. want to only replace the first occurence of a regular expression on
  59. each line, which is not easy to do when processing the file as a whole.
  60. Defaults to <i>false</i>.</td>
  61. <td valign="top" align="center">No</td>
  62. </tr>
  63. <tr>
  64. <td valign="top">encoding</td>
  65. <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
  66. <td align="center">No - defaults to default JVM encoding</td>
  67. </tr>
  68. </table>
  69. <h3>Examples</h3>
  70. <pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  71. match=&quot;OldProperty=(.*)&quot;
  72. replace=&quot;NewProperty=\1&quot;
  73. byline=&quot;true&quot;/&gt;
  74. </pre>
  75. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  76. with &quot;NewProperty&quot; in a properties file, preserving the existing
  77. value, in the file <code>${src}/build.properties</code></p>
  78. <h3>Parameters specified as nested elements</h3>
  79. <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
  80. element.</p>
  81. <p>This task supports a nested <i><a href="../CoreTypes/regexp.html">Regexp</a></i> element to specify
  82. the regular expression. You can use this element to refer to a previously
  83. defined regular expression datatype instance.</p>
  84. <blockquote>
  85. &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br />
  86. &lt;regexp refid="id"/&gt;
  87. </blockquote>
  88. <p>This task supports a nested <i>Substitution</i> element to specify
  89. the substitution pattern. You can use this element to refer to a previously
  90. defined substitution pattern datatype instance.</p>
  91. <blockquote>
  92. &lt;substitution id="id" expression="beta\1alpha"/&gt;<br />
  93. &lt;substitution refid="id"/&gt;
  94. </blockquote>
  95. <h3>Examples</h3>
  96. <blockquote>
  97. <pre>
  98. &lt;replaceregexp byline=&quot;true&quot;&gt;
  99. &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
  100. &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
  101. &lt;fileset dir=&quot;.&quot;&gt;
  102. &lt;includes=&quot;*.properties&quot;/&gt;
  103. &lt;/fileset&gt;
  104. &lt;/replaceregexp&gt;
  105. </pre></blockquote>
  106. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  107. with &quot;NewProperty&quot; in a properties file, preserving the existing
  108. value, in all files ending in <code>.properties</code> in the current directory</p>
  109. <blockquote>
  110. <pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
  111. &lt;fileset dir="${html.dir}" includes="**/*.html" /&gt;
  112. &lt;/replaceregexp&gt;
  113. </pre></blockquote>
  114. <p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
  115. line separator. So with input
  116. <blockquote>
  117. <pre>
  118. &lt;html> &lt;body&gt;
  119. &lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
  120. &lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
  121. </pre></blockquote>
  122. would converted to
  123. <pre>
  124. &lt;html&gt; &lt;body&gt;
  125. &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
  126. </pre>
  127. </p>
  128. <hr/>
  129. <p align="center">Copyright &copy; 2001-2004 The Apache Software Foundation. All rights
  130. Reserved.</p>
  131. </body>
  132. </html>