source: release-kits/lirk3/bin/ant-installer/web/manual1.6.2/manual/OptionalTasks/replaceregexp.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 7.6 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>. See details <a href="#implementation">below</a>. </p>
24
25<h3>Parameters</h3>
26<table border="1" cellpadding="2" cellspacing="0">
27 <tr>
28 <td valign="top"><b>Attribute</b></td>
29 <td valign="top"><b>Description</b></td>
30 <td align="center" valign="top"><b>Required</b></td>
31 </tr>
32 <tr>
33 <td valign="top">file</td>
34 <td valign="top">file for which the regular expression should be replaced.</td>
35 <td align="center">Yes if no nested &lt;fileset&gt; is used</td>
36 </tr>
37 <tr>
38 <td valign="top">match</td>
39 <td valign="top">The regular expression pattern to match in the file(s)</td>
40 <td align="center">Yes, if no nested &lt;regexp&gt; is used</td>
41 </tr>
42 <tr>
43 <td valign="top">replace</td>
44 <td valign="top">The substitution pattern to place in the file(s) in place
45 of the regular expression.</td>
46 <td align="center">Yes, if no nested &lt;substitution&gt; is used</td>
47 </tr>
48 <tr>
49 <td valign="top">flags</td>
50 <td valign="top">The flags to use when matching the regular expression. For more
51 information, consult the Perl5 syntax<br />
52 g : Global replacement. Replace all occurences found<br />
53 i : Case Insensitive. Do not consider case in the match<br />
54 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 />
55 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 />
56 </td>
57 <td valign="top" align="center">No</td>
58 </tr>
59 <tr>
60 <td valign="top">byline</td>
61 <td valign="top">Process the file(s) one line at a time, executing the replacement
62 on one line at a time (<i>true/false</i>). This is useful if you
63 want to only replace the first occurence of a regular expression on
64 each line, which is not easy to do when processing the file as a whole.
65 Defaults to <i>false</i>.</td>
66 <td valign="top" align="center">No</td>
67 </tr>
68 <tr>
69 <td valign="top">encoding</td>
70 <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
71 <td align="center">No - defaults to default JVM encoding</td>
72 </tr>
73</table>
74<h3>Examples</h3>
75<pre> &lt;replaceregexp file=&quot;${src}/build.properties&quot;
76 match=&quot;OldProperty=(.*)&quot;
77 replace=&quot;NewProperty=\1&quot;
78 byline=&quot;true&quot;/&gt;
79</pre>
80<p>replaces occurrences of the property name &quot;OldProperty&quot;
81 with &quot;NewProperty&quot; in a properties file, preserving the existing
82value, in the file <code>${src}/build.properties</code></p>
83<a name="implementation"/>
84<h3>Choice of regular expression implementation</h3>
85<p>
86Ant comes with
87wrappers for
88<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>,
89<a href="http://jakarta.apache.org/regexp/" target="_top">jakarta-regexp</a>
90and <a href="http://jakarta.apache.org/oro/" target="_top">jakarta-ORO</a>,
91See <a href="../install.html#librarydependencies">installation dependencies</a>
92 concerning the supporting libraries.</p>
93<p>
94The system property <code>ant.regexp.regexpimpl</code> governs which regular expression implementation will be chosen.
95Possible values for this property are :
96<ul>
97<li>
98org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
99</li>
100<li>
101org.apache.tools.ant.util.regexp.JakartaOroRegexp
102</li>
103<li>
104org.apache.tools.ant.util.regexp.JakartaRegexpRegexp
105</li>
106</ul>
107It can also be another implementation of the interface <code>org.apache.tools.ant.util.regexp.Regexp</code>.
108If <code>ant.regexp.regexpimpl</code> is not defined, ant checks in the order Jdk14Regexp, JakartaOroRegexp,
109 JakartaRegexp for the availability of the corresponding library. The first of these 3 which is found will be used.</p>
110<p>
111There are cross-platform issues for matches related to line terminator.
112For example if you use $ to anchor your regular expression on the end of a line
113the results might be very different depending on both your platform and the regular
114expression library you use. It is 'highly recommended' that you test your pattern on
115both Unix and Windows platforms before you rely on it.
116<ul>
117 <li>Jakarta Oro defines a line terminator as '\n' and is consistent with Perl.</li>
118 <li>Jakarta RegExp uses a system-dependant line terminator.</li>
119 <li>JDK 1.4 uses '\n', '\r\n', '\u0085', '\u2028', '\u2029' as a default
120 but is configured in the wrapper to use only '\n' (UNIX_LINE)</li>
121</ul>
122<em>We <b>strongly</b> recommend that you use Jakarta Oro.</em>
123</p>
124<h3>Parameters specified as nested elements</h3>
125<p>This task supports a nested <a href="../CoreTypes/fileset.html">FileSet</a>
126 element.</p>
127<p>This task supports a nested <i>Regexp</i> element to specify
128 the regular expression. You can use this element to refer to a previously
129 defined regular expression datatype instance.</p>
130<blockquote>
131 &lt;regexp id="id" pattern="alpha(.+)beta"/&gt;<br />
132 &lt;regexp refid="id"/&gt;
133</blockquote>
134<p>This task supports a nested <i>Substitution</i> element to specify
135 the substitution pattern. You can use this element to refer to a previously
136 defined substitution pattern datatype instance.</p>
137<blockquote>
138 &lt;substitution id="id" expression="beta\1alpha"/&gt;<br />
139 &lt;substitution refid="id"/&gt;
140</blockquote>
141<h3>Examples</h3>
142<blockquote>
143 <pre>
144&lt;replaceregexp byline=&quot;true&quot;&gt;
145 &lt;regexp pattern=&quot;OldProperty=(.*)&quot;/&gt;
146 &lt;substitution expression=&quot;NewProperty=\1&quot;/&gt;
147 &lt;fileset dir=&quot;.&quot;&gt;
148 &lt;includes=&quot;*.properties&quot;/&gt;
149 &lt;/fileset&gt;
150 &lt;/replaceregexp&gt;
151</pre></blockquote>
152<p>replaces occurrences of the property name &quot;OldProperty&quot;
153 with &quot;NewProperty&quot; in a properties file, preserving the existing
154value, in all files ending in <code>.properties</code> in the current directory</p>
155
156<blockquote>
157<pre>&lt;replaceregexp match="\s+" replace=" " flags="g" byline="true"&gt;
158 &lt;fileset dir="${html.dir}" includes="**/*.html" /&gt;
159&lt;/replaceregexp&gt;
160</pre></blockquote>
161<p>replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
162line separator. So with input
163
164<blockquote>
165<pre>
166&lt;html> &lt;body&gt;
167&lt;&lt;TAB&gt;&gt;&lt;h1&gt; T E S T &lt;/h1&gt; &lt;&lt;TAB&gt;&gt;
168&lt;&lt;TAB&gt;&gt; &lt;/body&gt;&lt;/html&gt;
169</pre></blockquote>
170would converted to
171<pre>
172&lt;html&gt; &lt;body&gt;
173 &lt;h1&gt; T E S T &lt;/h1&gt; &lt;/body&gt;&lt;/html&gt;
174</pre>
175</p>
176<hr/>
177<p align="center">Copyright &copy; 2001-2004 The Apache Software Foundation. All rights
178Reserved.</p>
179
180</body>
181</html>
182
Note: See TracBrowser for help on using the repository browser.