regex 文件中具有正则表达式匹配的ANT条件任务

y1aodyip  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在阅读ANT Conditions task,并试图根据在一个文件中找到的字符串找出如何使用它。
我有一个ANT构建,它有一系列replaceregexp目标,可以在目录中的任何.htm文件上运行,所以我指定文件集目录,然后指定regex模式和替换,并在该目录中的所有.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;\4&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=" "部分中输入什么?或者我如何给予它目录/文件来搜索匹配?
更新:
好的,这是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="\1\/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="\1\/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="\1\/refbody&gt;"/>
     <fileset dir=".">
     <include name="*.htm"/>
      <not>
     <contains text="ConceptStyle"/>
     </not>
     <not>
     <contains text="TaskChar"/>
     </not>
     </fileset>
   </replaceregexp> 
 </target>

每个目标操作一个文件集,过滤掉一个字符串,该字符串不能出现在文件中,以便目标操作它。有三个样式类型名称,我的源文件只包含其中一个(根据设计),现在这会触发ANT构建根据文件类型不同来处理每个文件。

q3qa4bjr

q3qa4bjr1#

首先,它看起来并不真的需要一个正则表达式,一个简单的<contains>可能会做到这一点-除非有文件的“概念样式”出现在身体之外。
AFAIU你想在所有文件上执行替换,除非它们包含ConceptStyle。我建议过滤fileset,而不是使用conditiontarget上的属性。

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

如果您真的需要正则表达式,那么使用<containsregexp>代替。

相关问题