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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. <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 &lt;fileset&gt; 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 &lt;regularexpression&gt; is used</td>
  37. </tr>
  38. <tr>
  39. <td valign="top">replace</td>
  40. <td valign="top">The substition pattern to place in the file(s) in place
  41. of the regular expression.</td>
  42. <td align="center">Yes, if no nested &lt;substitution&gt; 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 occurances 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 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 occurance 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. </table>
  64. <h3>Examples</h3>
  65. <pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
  66. match=&quot;OldProperty=(.*)&quot;
  67. replace=&quot;NewProperty=\1&quot;
  68. byline=&quot;true&quot; /&gt;
  69. </pre>
  70. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  71. with &quot;NewProperty&quot; in a properties file, preserving the existing
  72. value, in the file <code>${src}/build.properties</code></p>
  73. <h3>Parameters specified as nested elements</h3>
  74. <p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
  75. element.</p>
  76. <p>This task supports a nested <i>RegularExpression</i> element to specify
  77. the regular expression. You can use this element to refer to a previously
  78. defined regular expression datatype instance.</p>
  79. <blockquote>
  80. &lt;regularexpression id="id" pattern="expression" /&gt;<br />
  81. &lt;regularexpression refid="id" /&gt;
  82. </blockquote>
  83. <p>This task supports a nested <i>Substitution</i> element to specify
  84. the substitution pattern. You can use this element to refer to a previously
  85. defined substition pattern datatype instance.</p>
  86. <blockquote>
  87. &lt;substitution id="id" pattern="expression" /&gt;<br />
  88. &lt;substitution refid="id" /&gt;
  89. </blockquote>
  90. <h3>Examples</h3>
  91. <blockquote>
  92. <pre>
  93. &lt;replaceregexp byline=&quot;true&quot;&gt;
  94. &lt;regularexpression expression=&quot;OldProperty=(.*)&quot; /&gt;
  95. &lt;substitution expression=&quot;NewProperty=\1&quot; /&gt;
  96. &lt;fileset dir=&quot;.&quot;&gt;
  97. &lt;includes=&quot;*.properties&quot; /&gt;
  98. &lt;/fileset&gt;
  99. &lt;/replaceregexp&gt;
  100. </pre></blockquote>
  101. <p>replaces occurrences of the property name &quot;OldProperty&quot;
  102. with &quot;NewProperty&quot; in a properties file, preserving the existing
  103. value, in all files ending in <code>.properties</code> in the current directory</p>
  104. <hr>
  105. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  106. Reserved.</p>
  107. </body>
  108. </html>