regex - ANT 删除空格正则表达式

标签 regex ant spaces ant-contrib

我想获取 .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一次性转换,因为您只能指定 selectreplace<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/

相关文章:

PHP/正则表达式 : Get a string within parentheses

regex - 从识别命令解析图像大小

java - Ant 不重新编译常量

python - 删除文件python的空白空间

javascript - CSS 或 JS - 删除内联元素的默认横向空格

java - 正则表达式匹配斜杠的精确数量

python - 有没有一种优雅的方法来查看整个字符串是否只包含匹配项?

ant - 无法运行程序 "p4": CreateProcess error=2, 系统找不到指定的文件

grails - 在GANT中使用xmlProperty任务

java在字符串中左补零,中间有空格