source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/docs/manual/OptionalTasks/replaceregexp.html@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.8 KB
Line 
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
13occurrence of a given regular expression with a substitution pattern
14in a selected file or set of files.</p>
15
16<p>The output file is only written if it differs from the existing
17file. This prevents spurious rebuilds based on unchanged files which
18have been regenerated by this task.</p>
19
20<p>Similar to <a href="../CoreTypes/mapper.html#regexp-mapper">regexp
21type mappers</a> this task needs a supporting regular expression
22library and an implementation of
23<code>org.apache.tools.ant.util.regexp.Regexp</code>.
24See 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>&lt;fileset&gt;</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>&lt;regexp&gt;</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>&lt;substitution&gt;</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> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
77 match=&quot;OldProperty=(.*)&quot;
78 replace=&quot;NewProperty=\1&quot;
79 byline=&quot;true&quot;/&gt;
80</pre>
81<p>replaces occurrences of the property name &quot;OldProperty&quot;
82 with &quot;NewProperty&quot; in a properties file, preserving the existing
83value, 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 &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br />
93 &lt;regexp refid="id"/&gt;
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 &lt;substitution id="id" expression="beta\1alpha"/&gt;<br />
100 &lt;substitution refid="id"/&gt;
101</blockquote>
102<h3>Examples</h3>
103<blockquote>
104 <pre>
105&lt;replaceregexp byline=&quot;true&quot;&gt;
106 &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
107 &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
108 &lt;fileset dir=&quot;.&quot;&gt;
109 &lt;includes=&quot;*.properties&quot;/&gt;
110 &lt;/fileset&gt;
111 &lt;/replaceregexp&gt;
112</pre></blockquote>
113<p>replaces occurrences of the property name &quot;OldProperty&quot;
114 with &quot;NewProperty&quot; in a properties file, preserving the existing
115value, in all files ending in <code>.properties</code> in the current directory</p>
116
117<blockquote>
118<pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
119 &lt;fileset dir="${html.dir}" includes="**/*.html"/&gt;
120&lt;/replaceregexp&gt;
121</pre></blockquote>
122<p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
123line separator. So with input
124
125<blockquote>
126<pre>
127&lt;html&gt; &lt;body&gt;
128&lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
129&lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
130</pre></blockquote>
131would converted to
132<pre>
133&lt;html&gt; &lt;body&gt;
134 &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
135</pre>
136</p>
137<hr/>
138<p align="center">Copyright &copy; 2001-2004 The Apache Software Foundation. All rights
139Reserved.</p>
140
141</body>
142</html>
143
Note: See TracBrowser for help on using the repository browser.