我想获取 .doc 文件名并将其保存在下一个属性 (pdf.name
) 中。使用 regexp 我想删除 .doc 文件名中的所有空格并将其转换为:
NAME FILE.doc
对此:
NAMEFILE.pdf
这是我的代码:
<propertyregex override="yes" property="pdf.name" input="@{remoteDocToPdf}"
regexp="\.*([[^/]+$^\.]*)\.doc" select="\1.pdf" casesensitive="true" />
最佳答案
如果@{remoteDocToPdf}
仅包含文件名而不是绝对路径,您可以通过在发布的指令后添加此指令来删除文件名中的空格:
<propertyregex override="yes" property="pdf.name" input="${pdf.name}"
regexp=" " replace="" global="true" />
不可能删除空格并执行 .doc
-> .pdf
一次性转换,因为您只能指定 select
或 replace
每<propertyregex...
.
编辑 1: 我错过了添加 global="true"
如上所述,因此只有第一个空格会被替换(至少根据 documentation )。
编辑 2:关于 <propertyregex...
的注释你发布了 - 我很确定正则表达式 \.*([[^/]+$^\.]*)\.doc
并不是您真正想要的,即使它看起来可以按预期工作。从你的评论我猜你想要做的就是替换 .doc
与 .pdf
.在这种情况下,请改用它:
<propertyregex override="yes" property="pdf.name" input="@{remoteDocToPdf}"
regexp="\.doc$" replace=".pdf" />
如果您想深入了解正则表达式,我建议您阅读 this tutorial .
关于regex - ANT 删除空格正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584298/