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