0

我有一个文件有很多行,例如:

aaa  bbb  ccc  ddd  eeee
a111 b111 c111 d111
A222 B222 X222 Y222 Z222
FFF  GGG  HHH

ETC

我希望用ant来提取这个文件中的所有子字符串

aaa  bbb  ccc  ddd  eeee
a111 b111 c111 d111
A222 B222 X222 Y222 Z222
FFF  GGG  HHH

在每一行中,即第一行中的所有 aaa bbb ccc ddd eee,最后一行中的所有 FFF GGG HHH 等

请帮助如何做到这一点,但不使用正则表达式???

我的代码是:

<for list="@{line}"  delimiter=" " param = "val">
                <sequential>
                    <echo>in sequential : @{line}</echo>
                        <var name="first.part" value="1"/>
                        <var name="second.part" value="2"/>
                        <var name="third.part" value="3"/>
                        <propertyregex
                            property="first.part"
                            input="@{line}"
                            select="\0"/>
                        <propertyregex
                            property="second.part"
                            input="@{line}"
                            select="\1"/>
                        <propertyregex
                            property="third.part"
                            input="@{line}"
                            select="\2"/>
                        <echo> after assign: 1st.part = ${first.part}</echo>
                        <echo> after assign: 2nd.part = ${second.part}</echo>
                        <echo> after assign: 3rd.part = ${third.part}</echo>
            </sequential>

</for>

但是 ant 1.6 不支持正则表达式,我得到错误:该类型不支持“正则表达式”属性。

如何解决?谢谢

4

1 回答 1

1

ANT 不是一种编程语言。最好嵌入一个。

以下示例使用groovy

<project name="demo" default="scan">

    <target name="bootstrap">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.3/groovy-all-2.1.3.jar"/>
    </target>

    <target name="scan">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
        <groovy>
        new File("File1.txt").eachLine {
            def values = it.split()

            ant.echo "Extracted values: ${values}"

            values.each {
                ant.echo "Need to so something with ${it}"
            }
        }
        </groovy>
    </target>

</project>

产生以下输出

scan:
     [echo] Extracted values: [aaa, bbb, ccc, ddd, eeee]
     [echo] Need to so something with aaa
     [echo] Need to so something with bbb
     [echo] Need to so something with ccc
     [echo] Need to so something with ddd
     [echo] Need to so something with eeee
     [echo] Extracted values: [a111, b111, c111, d111]
     [echo] Need to so something with a111
     [echo] Need to so something with b111
     [echo] Need to so something with c111
     [echo] Need to so something with d111
     [echo] Extracted values: [A222, B222, X222, Y222, Z222]
     [echo] Need to so something with A222
     [echo] Need to so something with B222
     [echo] Need to so something with X222
     [echo] Need to so something with Y222
     [echo] Need to so something with Z222
     [echo] Extracted values: [FFF, GGG, HHH]
     [echo] Need to so something with FFF
     [echo] Need to so something with GGG
     [echo] Need to so something with HHH

更新

启用 groovy 任务的另一种方法是使用类路径引用,如下所示:

<path id="build.path">
    <pathelement location="/path/to/groovy-all/jar/groovy-all-2.1.3.jar"/>
</path>

<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
于 2013-05-26T10:49:54.400 回答