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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Ant User Manual</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>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
  13. type mappers</a> this task needs a supporting regular expression
  14. library and an implementation of
  15. <code>org.apache.tools.ant.util.regexp.Regexp</code>. Ant comes with
  16. implementations for
  17. <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>,
  18. <a href="http://jakarta.apache.org/regexp/" target="_top">jakarta-regexp</a>
  19. and <a href="http://jakarta.apache.org/oro/" target="_top">jakarta-ORO</a>,
  20. but you will still need the library itself.</p>
  21. <p>
  22. <i>
  23. There are cross-platform issues for matches related to line terminator.
  24. For example if you use $ to anchor your regular expression on the end of a line
  25. the results might be very different depending on both your platform and the regular
  26. expression library you use. It is 'highly recommended' to tests your pattern on
  27. Unix and Windows platforms before you rely on it.
  28. <ul>
  29. <li>Jakarta Oro defines a line terminator as '\n' and is consistent with Perl.</li>
  30. <li>Jakarta RegExp uses a system-dependant line terminator.</li>
  31. <li>JDK 1.4 uses '\n', '\r\n', '\u0085', '\u2028', '\u2029' as a default
  32. but is configured in the wrapper to use only '\n' (UNIX_LINE)</li>
  33. </ul>
  34. It is <b>strongly</b> recommended to use Jakarta Oro.
  35. </i>
  36. </p>
  37. <h3>Parameters</h3>
  38. <table border="1" cellpadding="2" cellspacing="0">
  39. <tr>
  40. <td valign="top"><b>Attribute</b></td>
  41. <td valign="top"><b>Description</b></td>
  42. <td align="center" valign="top"><b>Required</b></td>
  43. </tr>
  44. <tr>
  45. <td valign="top">file</td>
  46. <td valign="top">file for which the regular expression should be replaced.</td>
  47. <td align="center">Yes if no nested &lt;fileset&gt; is used</td>
  48. </tr>
  49. <tr>
  50. <td valign="top">match</td>
  51. <td valign="top">The regular expression pattern to match in the file(s)</td>
  52. <td align="center">Yes, if no nested &lt;regularexpression&gt; is used</td>
  53. </tr>
  54. <tr>
  55. <td valign="top">replace</td>
  56. <td valign="top">The substition pattern to place in the file(s) in place
  57. of the regular expression.</td>
  58. <td align="center">Yes, if no nested &lt;substitution&gt; is used</td>
  59. </tr>
  60. <tr>
  61. <td valign="top">flags</td>
  62. <td valign="top">The flags to use when matching the regular expression. For more
  63. information, consult the Perl5 syntax<br />
  64. g --> Global replacement. Replace all occurances found<br />
  65. i --> Case Insensitive. Do not consider case in the match<br />
  66. 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 />
  67. 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 />
  68. <td valign="top" align="center">No</td>
  69. </tr>
  70. <tr>
  71. <td valign="top">byline</td>
  72. <td valign="top">Process the file(s) one line at a time, executing the replacement
  73. on one line at a time (<i>true/false</i>). This is useful if you
  74. want to only replace the first occurance of a regular expression on
  75. each line, which is not easy to do when processing the file as a whole.
  76. Defaults to <i>false</i>.</td>
  77. <td valign="top" align="center">No</td>
  78. </tr>
  79. </table>
  80. <h3>Examples</h3>
  81. <pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  82. match=&quot;OldProperty=(.*)&quot;
  83. replace=&quot;NewProperty=\1&quot;
  84. byline=&quot;true&quot; /&gt;
  85. </pre>
  86. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  87. with &quot;NewProperty&quot; in a properties file, preserving the existing
  88. value, in the file <code>${src}/build.properties</code></p>
  89. <h3>Parameters specified as nested elements</h3>
  90. <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
  91. element.</p>
  92. <p>This task supports a nested <i>RegularExpression</i> element to specify
  93. the regular expression. You can use this element to refer to a previously
  94. defined regular expression datatype instance.</p>
  95. <blockquote>
  96. &lt;regularexpression id="id" pattern="expression" /&gt;<br />
  97. &lt;regularexpression refid="id" /&gt;
  98. </blockquote>
  99. <p>This task supports a nested <i>Substitution</i> element to specify
  100. the substitution pattern. You can use this element to refer to a previously
  101. defined substition pattern datatype instance.</p>
  102. <blockquote>
  103. &lt;substitution id="id" pattern="expression" /&gt;<br />
  104. &lt;substitution refid="id" /&gt;
  105. </blockquote>
  106. <h3>Examples</h3>
  107. <blockquote>
  108. <pre>
  109. &lt;replaceregexp byline=&quot;true&quot;&gt;
  110. &lt;regularexpression expression=&quot;OldProperty=(.*)&quot; /&gt;
  111. &lt;substitution expression=&quot;NewProperty=\1&quot; /&gt;
  112. &lt;fileset dir=&quot;.&quot;&gt;
  113. &lt;includes=&quot;*.properties&quot; /&gt;
  114. &lt;/fileset&gt;
  115. &lt;/replaceregexp&gt;
  116. </pre></blockquote>
  117. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  118. with &quot;NewProperty&quot; in a properties file, preserving the existing
  119. value, in all files ending in <code>.properties</code> in the current directory</p>
  120. <hr>
  121. <p align="center">Copyright &copy; 2001-2002 Apache Software Foundation. All rights
  122. Reserved.</p>
  123. </body>
  124. </html>