给定一个 CSV ant 属性,
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
我怎样才能得到第一个元素(即这里的“mod1”)?我想执行一个将“mod1”作为参数之一的命令。
此外..我无法将这个原始的“module.list”属性修改为列表或其他任何内容..尽管我可以从中创建另一个列表、属性等..
任何帮助表示赞赏.. 谢谢
最佳答案
这是实现您所描述的内容的一种方法。
- 将 CSV 写入临时文件
- 用replaceregexp解析它
- 将清理文件的内容读入新属性
- 删除临时文件
<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/