2001-11-12 11:26:48 +00:00
< html >
< head >
< meta http-equiv = "Content-Language" content = "en-us" >
2003-06-24 16:07:32 +00:00
< title > ReplaceRegExp Task< / title >
2001-11-12 11:26:48 +00:00
< / head >
< body >
2003-11-24 21:56:53 +00:00
< h2 > < a name = "replaceregexp" > ReplaceRegExp< / a > < / h2 >
2001-11-12 11:26:48 +00:00
< h3 > Description< / h3 >
< p > ReplaceRegExp is a directory based task for replacing the
occurrence of a given regular expression with a substitution pattern
in a selected file or set of files.< / p >
2003-07-23 12:57:18 +00:00
< p > The output file is only written if it differs from the existing
file. This prevents spurious rebuilds based on unchanged files which
have been regenerated by this task.< / p >
2001-11-12 11:26:48 +00:00
< p > Similar to < a href = "../CoreTypes/mapper.html#regexp-mapper" > regexp
type mappers< / a > this task needs a supporting regular expression
library and an implementation of
2003-12-01 22:03:25 +00:00
< code > org.apache.tools.ant.util.regexp.Regexp< / code > .
2004-04-20 12:48:43 +00:00
See details in the documentation of the < a href = "../CoreTypes/regexp.html#implementation" > Regexp Type< / a > . < / p >
2001-11-12 11:26:48 +00:00
< h3 > Parameters< / h3 >
< table border = "1" cellpadding = "2" cellspacing = "0" >
< tr >
< td valign = "top" > < b > Attribute< / b > < / td >
< td valign = "top" > < b > Description< / b > < / td >
< td align = "center" valign = "top" > < b > Required< / b > < / td >
< / tr >
< tr >
< td valign = "top" > file< / td >
< td valign = "top" > file for which the regular expression should be replaced.< / td >
< td align = "center" > Yes if no nested < fileset> is used< / td >
< / tr >
< tr >
< td valign = "top" > match< / td >
< td valign = "top" > The regular expression pattern to match in the file(s)< / td >
2002-02-19 16:48:47 +00:00
< td align = "center" > Yes, if no nested < regexp> is used< / td >
2001-11-12 11:26:48 +00:00
< / tr >
< tr >
< td valign = "top" > replace< / td >
2002-02-03 21:26:18 +00:00
< td valign = "top" > The substitution pattern to place in the file(s) in place
2001-11-12 11:26:48 +00:00
of the regular expression.< / td >
< td align = "center" > Yes, if no nested < substitution> is used< / td >
< / tr >
< tr >
< td valign = "top" > flags< / td >
< td valign = "top" > The flags to use when matching the regular expression. For more
information, consult the Perl5 syntax< br / >
2002-07-09 21:06:15 +00:00
g : Global replacement. Replace all occurences found< br / >
i : Case Insensitive. Do not consider case in the match< br / >
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 / >
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 / >
2003-11-24 21:56:53 +00:00
< / td >
2001-11-12 11:26:48 +00:00
< td valign = "top" align = "center" > No< / td >
< / tr >
< tr >
< td valign = "top" > byline< / td >
< td valign = "top" > Process the file(s) one line at a time, executing the replacement
on one line at a time (< i > true/false< / i > ). This is useful if you
2002-02-03 21:26:18 +00:00
want to only replace the first occurence of a regular expression on
2001-11-12 11:26:48 +00:00
each line, which is not easy to do when processing the file as a whole.
Defaults to < i > false< / i > .< / td >
< td valign = "top" align = "center" > No< / td >
< / tr >
2003-01-31 12:15:18 +00:00
< tr >
< td valign = "top" > encoding< / td >
< td valign = "top" > The encoding of the file. < em > since Ant 1.6< / em > < / td >
< td align = "center" > No - defaults to default JVM encoding< / td >
< / tr >
2001-11-12 11:26:48 +00:00
< / table >
< h3 > Examples< / h3 >
2002-01-30 21:32:08 +00:00
< pre > < replaceregexp file=" ${src}/build.properties"
2001-11-12 11:26:48 +00:00
match=" OldProperty=(.*)"
replace=" NewProperty=\1"
2002-09-04 11:05:19 +00:00
byline=" true" />
2001-11-12 11:26:48 +00:00
< / pre >
< p > replaces occurrences of the property name " OldProperty"
with " NewProperty" in a properties file, preserving the existing
value, in the file < code > ${src}/build.properties< / code > < / p >
2003-12-01 22:03:25 +00:00
2001-11-12 11:26:48 +00:00
< h3 > Parameters specified as nested elements< / h3 >
2002-01-30 21:32:08 +00:00
< p > This task supports a nested < a href = "../CoreTypes/fileset.html" > FileSet< / a >
2001-11-12 11:26:48 +00:00
element.< / p >
2003-12-01 22:03:25 +00:00
< p > This task supports a nested < i > < a href = "../CoreTypes/regexp.html" > Regexp< / a > < / i > element to specify
2001-11-12 11:26:48 +00:00
the regular expression. You can use this element to refer to a previously
defined regular expression datatype instance.< / p >
< blockquote >
2003-11-24 21:56:53 +00:00
< regexp id="id" pattern="alpha(.+)beta"/> < br / >
2002-09-04 11:05:19 +00:00
< regexp refid="id"/>
2001-11-12 11:26:48 +00:00
< / blockquote >
< p > This task supports a nested < i > Substitution< / i > element to specify
the substitution pattern. You can use this element to refer to a previously
2002-02-03 21:26:18 +00:00
defined substitution pattern datatype instance.< / p >
2001-11-12 11:26:48 +00:00
< blockquote >
2003-11-24 21:56:53 +00:00
< substitution id="id" expression="beta\1alpha"/> < br / >
2002-09-04 11:05:19 +00:00
< substitution refid="id"/>
2001-11-12 11:26:48 +00:00
< / blockquote >
< h3 > Examples< / h3 >
< blockquote >
< pre >
< replaceregexp byline=" true" >
2002-09-04 11:33:23 +00:00
< regexp pattern=" OldProperty=(.*)" />
2002-09-04 11:05:19 +00:00
< substitution expression=" NewProperty=\1" />
2001-11-12 11:26:48 +00:00
< fileset dir=" ." >
2002-09-04 11:05:19 +00:00
< includes=" *.properties" />
2001-11-12 11:26:48 +00:00
< /fileset>
< /replaceregexp>
< / pre > < / blockquote >
< p > replaces occurrences of the property name " OldProperty"
with " NewProperty" in a properties file, preserving the existing
value, in all files ending in < code > .properties< / code > in the current directory< / p >
2003-03-19 08:42:14 +00:00
< blockquote >
< pre > < replaceregexp match="\s+" replace=" " flags="g" byline="true">
< fileset dir="${html.dir}" includes="**/*.html" />
< /replaceregexp>
< / pre > < / blockquote >
< p > replaces all whitespaces (blanks, tabs, etc) by one blank remaining the
line separator. So with input
< blockquote >
< pre >
< html> < body>
< < TAB> > < h1> T E S T < /h1> < < TAB> >
< < TAB> > < /body> < /html>
< / pre > < / blockquote >
would converted to
< pre >
< html> < body>
< h1> T E S T < /h1> < /body> < /html>
< / pre >
2003-11-24 21:56:53 +00:00
< / p >
< hr / >
2004-02-09 21:50:10 +00:00
< p align = "center" > Copyright © 2001-2004 The Apache Software Foundation. All rights
2001-11-12 11:26:48 +00:00
Reserved.< / p >
< / body >
< / html >