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" />
Sergio
  • 28,539
  • 11
  • 85
  • 132

2 Answers2

1

If @{remoteDocToPdf} only carries a filename and not an absolute path, you can remove the spaces in the filename by adding this directive after the one you posted:

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

It's not possible to delete the spaces and do the .doc -> .pdf transformation in one go, since you can only either specify select or replace per <propertyregex....

Edit 1: I missed adding global="true" to the above, so only the first space would have been replaced (according to the documentation, at least).

Edit 2: A note on the <propertyregex... you've posted - I'm pretty sure that the regular expression \.*([[^/]+$^\.]*)\.doc is not really what you want, even though it may seem to work as intended. From your comment I'm guessing that all you want to do is replace .doc with .pdf. In that case, please use this instead:

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

If you'd like to read up on regular expressions, I can recommend reading this tutorial.

zb226
  • 9,586
  • 6
  • 49
  • 79
1

You might consider using Ant resources to do this. This might be close to what you need:

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

The work is being done by the filterchain comprised of two filters: one that removes spaces, and one that changes the file extension.

martin clayton
  • 76,436
  • 32
  • 213
  • 198