given file prep.js with contents: hi and the following build.xml <?xml version="1.0" encoding="UTF-8"?> <project name="abc" default="build"> <target name="build"> <replaceregexp file="./prep.js" match="(.*)" replace="\(abcd\)"/> </target> </project> running "ant" results in prep.js containing: (abcd) rather than the expected: \(abcd\) We ran into this bug in a production system using replaceregexp to insert the contents of one file into another. This method is widely recommended on the webs and will fail for any file containing, e.g. a regular expression matching a parenthesis (e.g. the javascript /\(/).
The problem is that the \ starts a backreference (like \1) so it must be quotet. Four slashes ... This one works for me: <target name="testMatching_Bug57293"> <mkdir dir="${output}"/> <echo file="${output}/text.txt">Hello, world!</echo> <replaceregexp match="(\w*)" replace="\\\\(abcd\\\\)"> <file file="${output}/text.txt"/> </replaceregexp> <au:assertResourceContains resource="${output}/text.txt" value="\(abcd\), world!"/> </target>
I am now thinking this should simply be mentioned in the documentation, as various people are using replaceregexp (in combination with loadfile) to insert files into other files, and I don't see any awareness of this issue among them.