Skip to main content
MarcioMarchiori
Silver ⭐️
December 9, 2021
Solved

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

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?

MattWho
MattWhoAnswer
Titanium ⭐️⭐️⭐️
December 9, 2021

@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

2 replies

MattWho
Titanium ⭐️⭐️⭐️
MattWhoAnswer
Titanium ⭐️⭐️⭐️
December 9, 2021

@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

Translate
MarcioMarchiori
Silver ⭐️
December 10, 2021

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

Translate