Reputation: 635
I want to make an ant target that can replace the content of a file (or more) that does not starts with a defined pattern. Is there a solution for this using ant and replaceregexp ? Or an alternative to this? Example:
I want to change the text for the foo.txt to: "this just foo"
Upvotes: 1
Views: 6386
Reputation: 78155
Probably a case of cooking up a suitable regular expression with captures.
This one appears to work for the example you give.
<property name="prefix" value="this " />
<replaceregexp flags="s" match="^(${prefix})?(.*)" replace="${prefix}\2">
<fileset dir="files" />
</replaceregexp>
files
directory.this
, which is stored in a property, as we need to mention it in two places.flags="s"
setting ensures that we treat the file as a single string for match purposes (rather than matching each line).\1
- which is discarded.\2
.\2
.You might think of it as always adding the prefix, but after taking the prefix away if it's already there...except that the replaceregexp
task will not write the file unless if differs from the existing.
Upvotes: 7
Reputation: 1110
this is just a partial answer.. I'm not sure if its correct but at least maybe can give you some direction
for this you need ant-contrib
<property name="folder.name" value="some-folder-name" />
<property name="text.substitution" value="this " />
<target name="main">
<foreach target="target2" param="file_path">
<path>
<fileset dir="${folder.name}" casesensitive="no" />
</path>
</foreach>
</target>
<target name="target2">
<loadfile property="message" srcFile="${file_path}"/>
<!-- f message doesnt have text.substitution at the begging -->
<!-- not sure if this is correct regex -->
<propertyregex property="myprop" input="${message}"
regexp="([^b]+)" select="\0" casesensitive="false" />
<if>
<equals arg1="${myprop}" arg2="{text.substitution}" />
<then>
<concat destfile="foo.txt" append="true">${text.substitution} </concat>
</then>
</if>
</target>
Upvotes: 0