java - 如何从 ant CSV 属性中提取第一个元素

标签 java list ant properties element

给定一个 CSV ant 属性,

<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

我怎样才能得到第一个元素(即这里的“mod1”)?我想执行一个将“mod1”作为参数之一的命令。

此外..我无法将这个原始的“module.list”属性修改为列表或其他任何内容..尽管我可以从中创建另一个列表、属性等..

任何帮助表示赞赏.. 谢谢

最佳答案

这是实现您所描述的内容的一种方法。

  1. 将 CSV 写入临时文件
  2. replaceregexp解析它
  3. 将清理文件的内容读入新属性
  4. 删除临时文件

<target name="parse" description="Example of how to parse the module.list property to extract the first value in the CSV"> 
    <property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

    <tempfile description="Generate a unique temporary filename for processing"  
    prefix="module.list" suffix="csv" destdir="${basedir}" property="tmpFile" />

    <concat description="Write the value of module.list to the temporary file" 
        destfile="${tmpFile}">${module.list}</concat>

    <replaceregexp description="filter the temporary file using the regex expression to find the first occurance of a , and all characters afer and replace with nothing"
        file="${tmpFile}"
        match=",.*"
        replace=""
        byline="true"/>

    <loadresource description="read the contents of the scrubbed temporary file into the mod1 property"
        property="mod1">
        <file file="${tmpFile}"/>
    </loadresource>

    <delete description="remove the temporary file" 
    file="${tmpFile}" />

    <!--Now you have the parsed value in the mod1 property -->
    <echo message="mod1=${mod1}" />

</target>

关于java - 如何从 ant CSV 属性中提取第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1349959/

相关文章:

ant - anthill和gradle插件?

java - 使用正则表达式过滤

javax.net.ssl.SSLException : Received fatal alert: bad_record_mac 异常

python - 我需要在 python 中合并子列表的元素

ant - 我是否需要在要部署文件的服务器上安装 Ant?

ant - Ant 可以使用来自 Pageant 的 SSH 加密私钥吗?

java - 我该如何进行这个单元测试?

Java程序确定二维数组的矩阵中是否有重复行?

python - 删除两个具有重复元素的列表之间的公共(public)元素

Java 8 - 获取列表元素的 'parent' 对象