gpt4 book ai didi

regex - 如何从存储在 Ant 属性中的文件路径中删除前缀?

转载 作者:行者123 更新时间:2023-12-04 18:13:52 24 4
gpt4 key购买 nike

我有一个文件路径,例如 /repo/java/projects/myproj在 Ant 属性中。如何删除 /repo/java并存储结果projects/myproj在另一个属性?

最佳答案

pathconvert Ant 任务可以与嵌套的 mapper 一起使用从路径中删除前两个目录(或获取前两个目录)。

删除前缀 /repo/java/
匹配'/'文件分隔符

<property name="path" value="/repo/java/projects/myproj"/>

<target name="test1">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^/[^/]+/[^/]+/(.*)"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>

输出
test1:
[echo] projects/myproj

BUILD SUCCESSFUL
Total time: 0 seconds

匹配平台文件分隔符
<property name="path" value="/repo/java/projects/myproj"/>

<target name="test2">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^${file.separator}[^${file.separator}]+${file.separator}[^${file.separator}]+${file.separator}(.*)"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>

输出
test2:
[echo] projects/myproj

BUILD SUCCESSFUL
Total time: 0 seconds

获取前缀 /repo/java/
匹配'/'文件分隔符
<property name="path" value="/repo/java/projects/myproj"/>

<target name="test3">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^(/[^/]+/[^/]+/).*"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>

输出
test3:
[echo] /repo/java/

BUILD SUCCESSFUL
Total time: 0 seconds

匹配平台特定的文件分隔符
<property name="path" value="/repo/java/projects/myproj"/>

<target name="test4">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^(${file.separator}[^${file.separator}]+${file.separator}[^${file.separator}]+${file.separator}).*"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>

输出
test4:
[echo] /repo/java/

BUILD SUCCESSFUL
Total time: 0 seconds

关于regex - 如何从存储在 Ant 属性中的文件路径中删除前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com