Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用jpg | png的Ant任务捕获图像扩展_Regex_Ant_Ant Contrib - Fatal编程技术网

Regex 使用jpg | png的Ant任务捕获图像扩展

Regex 使用jpg | png的Ant任务捕获图像扩展,regex,ant,ant-contrib,Regex,Ant,Ant Contrib,现在我更清楚地知道发生了什么,就重写这个: 我有一些Ant propertyregex任务,可以从文件中的图像链接中提取三个属性。然后我想用这些来重写链接 到目前为止,我所拥有的提取了我需要的属性,然后如果我使用replaceregex任务遵循这些属性,我可以使用这些属性重写链接。但是,即使replaceregex一次操作一行,完成一行,然后在下一行重新开始,它也会获取从第一个链接提取的属性,并将它们用作文件中每个链接的替换项 有没有一种方法可以将这两者结合起来,使整个操作从一个链接开始,提取属

现在我更清楚地知道发生了什么,就重写这个:

我有一些Ant propertyregex任务,可以从文件中的图像链接中提取三个属性。然后我想用这些来重写链接

到目前为止,我所拥有的提取了我需要的属性,然后如果我使用replaceregex任务遵循这些属性,我可以使用这些属性重写链接。但是,即使replaceregex一次操作一行,完成一行,然后在下一行重新开始,它也会获取从第一个链接提取的属性,并将它们用作文件中每个链接的替换项

有没有一种方法可以将这两者结合起来,使整个操作从一个链接开始,提取属性,重写它,然后移动到下一个链接并重新开始

以下是我得到的:

<project name="BuildModule" basedir="." default="extract.stuff">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>
<target name="extract.stuff">
    <for param="line" delimiter="${line.separator}" list="${file}">
        <sequential>

            <propertyregex property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
            <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
            <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

        </sequential>
    </for>


     <replaceregexp byline="true">
      <regexp pattern="(&lt;img class)(.*?$)"/>
     <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;acme_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>
     <fileset dir=".">
     <include name="**.log"/>
     </fileset>
   </replaceregexp>

</target>

注释掉
中的
是一种尝试,使所有内容都按照
循环中指定的输入运行,因为问题是使用文件集的
在返回到
部分之前逐行执行整个文件。但是现在,如果没有
,构建只会忽略
部分。如果我将
放回,它会像以前一样,将每个属性更改为文件中遇到的第一个属性


将覆盖添加到
修复了保留属性的问题,因此我不需要

在Ant中,一旦设置了属性,其设计是不可变的,因此您需要首先使用antcontrib取消设置属性。然后,您将在for循环中使用replaceregexp工具:

    <for param="line" delimiter="${line.separator}" list="${file}">
     <sequential>

      <var name="imageName" unset="true"/>
      <var name="width" unset="true"/>
      <var name="alt" unset="true"/>

      <propertyregex property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
      <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
      <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

      <replaceregexp>
       <!-- ... -->
      </replaceregexp>

     </sequential>
    </for>

循环在
之前已经结束,因此您没有为
任务的每一行提取
${imageName}、${width}和${alt}
的值。因此,由于缺少
override=“true”
,它们不会改变,可能会保留
通过的第一行的值

因此,可以将
任务包含在
中,如下一个答案所示,或者在
表达式中指定
图像名称、宽度和alt
。。。指定组,例如
\2、\5、\X..
,并将
模式字符串替换为包含文件整行的模式字符串

例如:

<regexp pattern="&lt;img class.*?/(.*?graphics)/(.*?)&quot; width=&quot;(.*?)&quot; -alt=&quot;(.*?)&quot;/&gt;"/>
<substitution expression="\&lt;ac:image ac:title=&quot;\4&quot; ac:&quot;\3&quot;&gt;&lt;ri:attachment ri:filename=&quot;\2&quot;&gt;&lt;ri:page ri:content-title=&quot;\1&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

谢谢,我曾尝试将replaceregexp放入
中,但出现了一个错误,但在阅读了您的回复和另一个回复后,我意识到它也需要放入
中\(编辑时间用完了…)但是现在,当使用您的建议时,我发现
无法创建未设置的类型或任务。名称未定义。
这是否意味着
取消设置
,或者我猜
变量
任务未包含在我的ant contrib库中?我将Ant版本作为OxygenXML的一部分安装,其中的Ant contrib jar似乎是1.0b3,不是最新版本,而是之前的版本。我在试图找出如何用下载来替换它时有点困惑,我在最新下载的1.0b5.Oops中没有看到.jar文件,对不起-当然必须改为=>在我的答案中修复它。非常感谢,请参阅下面我的评论。我可能也会尝试你的第二种解决方案,但我很好奇我是否能让变量工作,因为我可以在其他地方使用这种策略,也只是为了我自己的学习目的。
unset
可以用作
任务的一个属性。你需要包括ant-contrib.jar。例如:
${x}
${x}
for var&unset:for ant contrib here:Thx,在我再次阅读变量任务页面后,我意识到。好的,我似乎把ant contrib jar作为一个资源,因为
propertyregex
是一个ant contrib任务,它从一开始就在工作。如果我在项目中打开
之后插入
,或者在
中的任何其他地方插入
,似乎不会取消设置变量,行为与以前一样。如果我将其插入
之外的任何位置,我会收到错误。
似乎正在运行;作为测试,我再次将其插入
Thx之后。关于:
[…]。。。由于循环[…]的原因,将其更改为一次服务每一行。
如何?我也一直在想这个问题,所以我从
组中删除了所有
[…]
,但它只运行
部分
`现在位于
内部,因此也位于
内部,但是现在,如果没有,构建只会忽略该部分。。这是因为您没有向要匹配-n-replace的
任务提供任何输入(
之前以要操作的文件的形式提供输入)。因此,请这样做:
或取消对
的注释,并选择更改为该
,我建议早些时候删除
,以避免处理那里的额外文件(除了相关的
inputLog.log
和此类文件)。如果一个或多个文件中需要替换,请小心使用
标记,尽量避免不必要的
.log
文件位于
文件集basedir
中。您可以更改
正则表达式模式
以直接匹配
@{line}
..
,并且还包括
override=“true”
,我必须说,这种方法会起作用(可能需要一些调整),但我不会向您推荐。
${file}
上循环,在
循环的每次迭代中,
逐行扫描其输入文件,直到达到文件的匹配/结尾。使用分组方法可以完全避免这些额外的迭代。好的,谢谢大家的帮助。我现在明白了。我将使用GRU
<project name="BuildModule" basedir="." default="extract.stuff">

<taskdef resource="net/sf/antcontrib/antlib.xml" />

<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>

<target name="extract.stuff">

    <for param="line" delimiter="${line.separator}" list="${file}">

        <sequential>

        <propertyregex override="true" property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
        <propertyregex override="true" property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
        <propertyregex override="true" property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />  

        <replaceregexp byline="true">
        <regexp pattern="(&lt;img class)(.*?$)"/>
        <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;BSW_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

        <!-- 
        <fileset dir=".">
        <include name="**.log"/>
        </fileset>
        -->

        </replaceregexp>

        </sequential>

    </for>

</target>
    <for param="line" delimiter="${line.separator}" list="${file}">
     <sequential>

      <var name="imageName" unset="true"/>
      <var name="width" unset="true"/>
      <var name="alt" unset="true"/>

      <propertyregex property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
      <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
      <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

      <replaceregexp>
       <!-- ... -->
      </replaceregexp>

     </sequential>
    </for>
<regexp pattern="&lt;img class.*?/(.*?graphics)/(.*?)&quot; width=&quot;(.*?)&quot; -alt=&quot;(.*?)&quot;/&gt;"/>
<substitution expression="\&lt;ac:image ac:title=&quot;\4&quot; ac:&quot;\3&quot;&gt;&lt;ri:attachment ri:filename=&quot;\2&quot;&gt;&lt;ri:page ri:content-title=&quot;\1&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>