1

我想制作一个 ant 目标,它可以替换不以定义的模式开头的文件(或更多)的内容。有没有使用 ant 和 replaceregexp 的解决方案?或者这个的替代品?例子:

  • 我有文件 boo.txt,其中包含以下文本:“这是 boo”。
  • 我也有文件 foo.txt,其中包含文本:“just foo”。

我想将 foo.txt 的文本更改为:“this just foo”

4

2 回答 2

7

可能是用捕获来制作合适的正则表达式的情况。

这似乎适用于您给出的示例。

<property name="prefix" value="this " />
<replaceregexp flags="s" match="^(${prefix})?(.*)" replace="${prefix}\2">
    <fileset dir="files" />
</replaceregexp>
  • 这些文件是使用文件集指定的 - 这里是files目录中的所有文件。
  • 我们想要添加的前缀是this,它存储在一个属性中,因为我们需要在两个地方提到它。
  • flags="s"设置确保我们将文件视为单个字符串以进行匹配(而不是匹配每一行)。
  • 正则表达式在文件中查找前缀字符串,并将其存储在 capture \1- 被丢弃。
  • 字符串的其余部分在 capture 中\2
  • 替换为前缀字符串,后跟 capture \2

您可能会认为它总是添加前缀,但是如果前缀已经存在,则在删除前缀之后......除了replaceregexp任务不会写入文件,除非与现有文件不同。

于 2010-11-05T22:07:24.637 回答
0

这只是部分答案。我不确定它是否正确,但至少可以给你一些方向

为此,您需要 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>
于 2010-11-05T21:17:36.793 回答