Support Questions

Find answers, ask questions, and share your expertise

How to apply replaceAll to ${path} on NiFi?

avatar
New Contributor

Hello!

 

I'm getting files from a SMB with GetSMBFile, eventually they'll be placed on S3 with PutS3Object, the problem is the path contains "\" on it and I need to replace all of those to "/", I attempted a simple ${path:replaceAll('\','//')}/${filename} on the Object Key but it didn't work.

 

What's the proper way to do this on NiFi?

1 ACCEPTED SOLUTION

avatar
Master Mentor

@MarcioMarchiori 

Depending on which NiFi Expression Language (NEL) function you are using, either of the following two NEL statements should work for you:

${path:replaceAll("\\\\",'/')}/${filename}

In above you are using the replaceAll function which expects a java regular expression.  Since '\" is an escape character and NiFi is a java application, the first \ escapes the second \ and the third \ escapes the forth \.   So \\\\ results in a regex of \\ being applied against the value present in the "path" variable.


or

${path:replace('\\','/')}/${filename}

In above you are using the replace function which does not path the first argument to be evaluated as a java regular expression but rather takes a string literal.  And since '\' is an escape character you need \\ which tells java to treat the second \ as the literal string value to search for.

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt

View solution in original post

2 REPLIES 2

avatar
Master Mentor

@MarcioMarchiori 

Depending on which NiFi Expression Language (NEL) function you are using, either of the following two NEL statements should work for you:

${path:replaceAll("\\\\",'/')}/${filename}

In above you are using the replaceAll function which expects a java regular expression.  Since '\" is an escape character and NiFi is a java application, the first \ escapes the second \ and the third \ escapes the forth \.   So \\\\ results in a regex of \\ being applied against the value present in the "path" variable.


or

${path:replace('\\','/')}/${filename}

In above you are using the replace function which does not path the first argument to be evaluated as a java regular expression but rather takes a string literal.  And since '\' is an escape character you need \\ which tells java to treat the second \ as the literal string value to search for.

If you found this response assisted with your query, please take a moment to login and click on "Accept as Solution" below this post.

Thank you,

Matt

avatar
New Contributor

Thanks mate, I had completely forgotten about the need for the need for the '\\\\', that solved it.