我想制作一个 ant 目标,它可以替换不以定义的模式开头的文件(或更多)的内容。有没有使用 ant 和 replaceregexp 的解决方案?或者这个的替代品?例子:
- 我有文件 boo.txt,其中包含以下文本:“这是 boo”。
- 我也有文件 foo.txt,其中包含文本:“just foo”。
我想将 foo.txt 的文本更改为:“this just foo”
我想制作一个 ant 目标,它可以替换不以定义的模式开头的文件(或更多)的内容。有没有使用 ant 和 replaceregexp 的解决方案?或者这个的替代品?例子:
我想将 foo.txt 的文本更改为:“this just foo”
可能是用捕获来制作合适的正则表达式的情况。
这似乎适用于您给出的示例。
<property name="prefix" value="this " />
<replaceregexp flags="s" match="^(${prefix})?(.*)" replace="${prefix}\2">
<fileset dir="files" />
</replaceregexp>
files
目录中的所有文件。this
,它存储在一个属性中,因为我们需要在两个地方提到它。flags="s"
设置确保我们将文件视为单个字符串以进行匹配(而不是匹配每一行)。\1
- 被丢弃。\2
。\2
。您可能会认为它总是添加前缀,但是如果前缀已经存在,则在删除前缀之后......除了replaceregexp
任务不会写入文件,除非与现有文件不同。
这只是部分答案。我不确定它是否正确,但至少可以给你一些方向
为此,您需要 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>