You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ant中从字符串属性提取首行内容?

提取多行字符串属性的首行内容(Ant脚本方案)

你当前的<for>循环会遍历属性的每一行并输出,所以会打印全部内容。要只提取首行,不需要遍历整个列表,以下两种方法可以直接实现需求:

方法一:使用propertyregex正则截取

<propertyregex property="firstLine"
               input="${targetDeviceUDID}"
               regexp="^(.*?)\r?\n"
               select="\1"
               casesensitive="false"/>
<echo>${firstLine}</echo>

正则表达式^(.*?)\r?\n会匹配从字符串开头到第一个换行符的内容(兼容Windows的\r\n和Unix的\n换行格式),将首行内容提取到firstLine属性中。

方法二:使用loadresource配合headfilter

<loadresource property="firstLine">
    <string value="${targetDeviceUDID}"/>
    <filterchain>
        <headfilter lines="1"/>
    </filterchain>
</loadresource>
<echo>${firstLine}</echo>

headfilter lines="1"会直接读取字符串的第一行内容,存入firstLine属性,这种方式更直观,不需要写正则。

内容的提问来源于stack exchange,提问作者Santanu Karar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:40:29