ANT 条件任务与文件中的正则表达式匹配

ANT Conditions task with regex match in a file

我正在阅读 ANT Conditions task 并试图根据在一个文件中找到的字符串弄清楚如何使用它。

我有一个 ANT 构建,其中包含一系列 replaceregexp 目标,它在目录中的任何 .htm 文件上运行。所以我指定文件集目录,然后是正则表达式模式和替换,并在该目录中的所有 .htm 文件中进行替换。例如:

  <target name="title" description="convert title">
   <replaceregexp byline="false" flags="gs">
     <regexp pattern="(&lt;p.*?)(TopicTitle&gt;.*?)(&gt;)(.*?)(&lt;/span.*?/p&gt;)"/>
     <substitution expression="&lt;title&gt;&lt;/title&gt;"/>
     <fileset dir=".">
     <include name="*.htm"/>
     </fileset>
   </replaceregexp> 
 </target>

现在我要做的是使用正则表达式查看 .htm 文件中是否有某个单词,如果匹配则设置条件。例如:

<condition property="isConcept">
  <matches pattern="&lt;body.*(?=ConceptStyle).*&lt;/body&gt;" string=" "/>
</condition>

因此,如果字符串 ConceptStyle 与该正则表达式匹配,则设置 属性 isConcept

我的问题是:我在该任务的 string=" " 部分放了什么?或者我如何给它 directory/file 来搜索匹配项?

更新:

好的,这是下面 Manouti 的回答中的新代码:

<target name="setProperties">
  <loadfile property="contents" srcFile="C:\Developer\SVN\trunk\WordTemplates\conversions\Conversion_Demo.htm"/>
  <condition property="isConcept">
  <matches pattern="&lt;body.*(?=ConceptStyle).*&lt;/body&gt;" string="${contents}"/>
   </condition>
</target>

为了测试是否设置了属性,我的理解是我可以将unless添加到另一个目标,像这样:

<!-- sticking in a replace task to test if condition is set -->
<target name="conditionTest" unless="isConcept">    
  <replaceregexp byline="false" flags="gs">
     <regexp pattern="conbody"/>
     <substitution expression="BOOOO"/>
     <fileset dir=".">
     <include name="*.htm"/>
     </fileset>
   </replaceregexp> 
 </target>

到目前为止这不起作用,也就是说,包含 unless="isConcept" 的目标正在运行("BOOOO" 出现在结果中),而如果 属性 是设置,它不应该开火。

我曾将 unless 用于其他目的,但仅限于它基于启动 ANT 构建的命令中的标志的情况。然而,从我读过的内容来看,这看起来应该可行。我有什么问题吗?

更新二

好的,根据 Stefan 的回答,这工作得很好:

  <target name="Concept3" description="Insert closing body tag" depends="Concept2,Concept1,Concept0">
   <replaceregexp byline="false" flags="gs">
     <regexp pattern="(&lt;)(/body)(&gt;)"/>
     <substitution expression="\/conbody&gt;"/>
     <fileset dir=".">
     <include name="*.htm"/>
    <not>
     <contains text="TaskChar"/>
     </not>
     <not>
     <contains text="RefChar"/>
     </not>
     </fileset>
   </replaceregexp> 
 </target>

 <target name="Task3" description="" depends="Task2,Task1,Task0">
   <!-- Insert closing body tag-->
   <replaceregexp byline="false" flags="gs">
     <regexp pattern="(&lt;)(/body)(&gt;)"/>
     <substitution expression="\/taskbody&gt;"/>
     <fileset dir=".">
     <include name="*.htm"/>
     <not>
     <contains text="ConceptStyle"/>
     </not>
     <not>
     <contains text="RefChar"/>
     </not>
     </fileset>
   </replaceregexp> 
 </target>

 <target name="Ref3" description="" depends="Ref2,Ref1,Ref0">
   <!-- Insert closing body tag-->
   <replaceregexp byline="false" flags="gs">
     <regexp pattern="(&lt;)(/body)(&gt;)"/>
     <substitution expression="\/refbody&gt;"/>
     <fileset dir=".">
     <include name="*.htm"/>
      <not>
     <contains text="ConceptStyle"/>
     </not>
     <not>
     <contains text="TaskChar"/>
     </not>
     </fileset>
   </replaceregexp> 
 </target>

每个目标都对一个文件集进行操作,该文件集已针对不得出现在文件中的字符串进行过滤,以便目标对其执行操作。有三种样式类型名称,而我的源文件仅包含这三种类型中的一种(按设计),这现在会触发 ANT 构建以根据文件类型不同来处理每个文件。伟大的。

首先,看起来您根本不需要正则表达式,一个简单的 <contains> 可能就可以了 - 除非有 "ConceptStyle" 出现在正则表达式之外的文件正文。

AFAIU 您想对所有文件执行替换,除非它们包含 ConceptStyle。而不是在 target 上使用 condition 和属性,我建议改为过滤 fileset

<replaceregexp byline="false" flags="gs">
  <regexp pattern="conbody"/>
  <substitution expression="BOOOO"/>
  <fileset dir=".">
    <include name="*.htm"/>
    <not>
      <contains text="ConceptStyle">
    </not>
  </fileset>
</replaceregexp>

如果您确实需要正则表达式,请改用 <containsregex>