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

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