【问题标题】:Ant ReplaceRegExp task - multiple substitutions per file?Ant ReplaceRegExp 任务 - 每个文件多个替换?
【发布时间】:2016-08-01 16:52:51
【问题描述】:

我正在使用 Ant ReplaceRegExp 任务来替换 10,000 多个文件中的文本。每个文件都有我需要执行的多个搜索/替换模式。截至目前,有多个块,每个块都有一个“**/*.java”的文件集。

随着我要搜索/替换的内容列表的增加,这需要永远运行,因为它将在 10k 文件上执行 ReplaceRegExp #1,然后在 10k 文件上执行 ReplaceRegExp2,依此类推。我已经有大约 15 个 ReplaceRegExp 块,所以我的任务已经花费了大约 45 分钟。

有没有办法让每个文件只处理/访问一次,并且它对文件执行所有 15 次以上的正则表达式搜索/替换,而不是必须单独处理 15 次以上的文件?

本质上,寻找逻辑上是这样的东西:

    <replaceregexp  
        match="firstmatch"  
        replace="firstraplce"

        match="secondmatch"
        replace="secondreplace"

        etc..

        byline="true">
        <fileset dir=".">
            <include name="**/*.java"/>
        </fileset>
    </replaceregexp>

这显然是无效的 XML,但从概念上显示了我正在寻找的内容。现在我在同一组文件上使用 2 个组 - 一个用于“firstmatch/firstreplace”,一个用于“secondmatch/secondreplace”。

谢谢。

【问题讨论】:

    标签: regex ant


    【解决方案1】:

    以下作品:

     <filterchain>
        <tokenfilter>
            <replacestring from="oldstr1" to="newstr1"/>
            <replacestring from="oldstr2" to="newstr2"/>
        </tokenfilter>
     </filterchain>
    

    【讨论】:

      【解决方案2】:

      您可以使用FilterChain and FilterReader 中的TokenFilter,它允许定义一个分词器(在您的情况下是一个行分词器)和一组将按顺序应用的过滤器。不知道它是否比调用 n 次 ReplaceRegExp 任务有更好的性能,但你应该试试:

      <filterchain>
          <tokenfilter>
              <replaceregex pattern="match_1" replace="replace_1"/>
              <replaceregex pattern="match_2" replace="replace_2"/>
              ...
              <replaceregex pattern="match_n" replace="replace_n"/>
          </tokenfilter>
      </filterchain>
      

      【讨论】:

      • 这会返回错误:tokenfilter 不支持嵌套的“replaceregexp”元素
      • @Heinz 感谢您指出这个错误,这是一个错字...我已经更正了示例,现在可以使用...
      猜你喜欢
      • 2013-08-29
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多