Subtituting Text Using Regular Expressions
The replaceregexp task
can replace every occurrence of a given regular expression with a
substitution pattern in a selected file or set of files.
Tip
The output file is written only if it differs from the existing file.
Example 10-2 uses
replaceregexp, where text in the XML
comment in the build file matching "Here's a comment." is converted to
"Here's an XML comment."
Example 10-2. Using regular expression substitutions (ch10/regexp/build.xml)
<?xml version="1.0"?>
<project default="main" basedir=".">
<!--Here's a comment.-->
<target name="main">
<replaceregexp
match="a comment"
replace="an XML comment">
<fileset dir="." includes="**/*.xml" />
</replaceregexp>
</target>
</project>Here's what the build file looks like after you run it:
<?xml version="1.0"?>
<project default="main" basedir=".">
<!--Here's an XML comment.-->
<target name="main">
<replaceregexp
match="a comment"
replace="an XML comment">
<fileset dir="." includes="**/*.xml" />
</replaceregexp>
</target>
</project>Changing the contents of other files this way will be
useful if you need to rewrite build files that you're about to call with
the ant task. Here's an example of
editing build files to convert from a local to an FTP install:
<?xml version="1.0"?>
<project default="main" basedir=".">
<target name="main">
<replaceregexp
match="<copy file='Project.jar' todir='dist'/>"
replace="<ftp server='ftp.isp.com'><fileset dir='bin'/></ftp>"
<fileset dir="subproject" includes="**/*.xml" ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access