3

I want to get the .doc file name and save it in the next property (pdf.name). Using regexp I want to remove all the blank spaces in the .doc file name and transform it from this:

NAME FILE.doc

To this:

NAMEFILE.pdf

This is my code:

<propertyregex override="yes" property="pdf.name" input="@{remoteDocToPdf}" 
     regexp="\.*([[^/]+$^\.]*)\.doc" select="\1.pdf" casesensitive="true" />
4

2 回答 2

1

您可以考虑使用 Ant资源来执行此操作。这可能接近您的需要:

<loadresource property="pdf.name">
    <string value="${remoteDocToPdf}" />
    <filterchain>
        <deletecharacters chars=" " />
        <replaceregex pattern=".doc$" replace=".pdf" />
    </filterchain>
</loadresource>

这项工作由包含两个过滤器的过滤器完成:一个用于删除空格,另一个用于更改文件扩展名。

于 2012-11-27T19:46:10.687 回答
1

如果@{remoteDocToPdf}仅带有文件名而不是绝对路径,则可以通过在您发布的指令之后添加此指令来删除文件名中的空格:

<propertyregex override="yes" property="pdf.name" input="${pdf.name}"
    regexp=" " replace="" global="true" />

不可能一次性删除空格并进行.doc->.pdf转换,因为您只能指定selectreplaceper <propertyregex...

编辑 1:我错过了添加global="true"到上面的内容,所以只有第一个空格会被替换(至少根据文档)。

编辑 2:关于<propertyregex...您发布的注释 - 我很确定正则表达式\.*([[^/]+$^\.]*)\.doc并不是您真正想要的,即使它似乎按预期工作。从您的评论中,我猜您想要做的就是替换.doc.pdf. 在这种情况下,请改用这个:

<propertyregex override="yes" property="pdf.name" input="@{remoteDocToPdf}" 
    regexp="\.doc$" replace=".pdf" />

如果您想阅读正则表达式,我可以推荐阅读本教程

于 2012-11-27T14:36:20.013 回答