Search code examples
regexapache-nifihortonworks-data-platform

Use Nifi replaceText to swap first or last occurance of char/string with another string?


Trying to add tag to incoming nifi json flowfile.

Input:

[{"HIT":"DUMMY_3","BatchId":"jkajks981n-1280189nd-129dnbj-2349nbfk","Id":"81274376231"}]

Expected output:

[{"nifi_received_ts_est":"2018-10-04 09:31:50.108","HIT":"DUMMY_3","BatchId":"jkajks981n-1280189nd-129dnbj-2349nbfk","Id":"81274376231"}]

Tried different methods and am close to this now:

Search Value: ^([^\[]*)
Replacement Value: [{"nifi_received_ts_est":"${now():format("yyyy-MM-dd HH:mm:ss.SS")}"\,$2
Replacement Strategy: Regex Replace
Evaluation Mode: Entire Text

But result is not what is expected. getting below:

[{"nifi_received_ts_est":"2018-10-04 09:31:50.108",$2[{"HIT":"DUMMY_3","BatchId":"jkajks981n-1280189nd-129dnbj-2349nbfk","Id":"81274376231"}]

Never was good in regexp... :( can someone help with the correct phrase to search and replace? Also if someone can explain the regexp and how grouping is done it would help as well. Perhaps a good cheatsheet reference. What is a good site to test and parse nifi specific regexp expressions?

SOLUTION: If it helps anyone found the pattern: Search Value: ^(.*?)[{ (will do lazy search until finds first '[{' and groups stuff before it to $1) so replacement will be: $1{"nifi_received_ts_est":"${now():format("yyyy-MM-dd HH:mm:ss.SS")}",


Solution

  • change the search value to ^(\[\{)(.*)

    in this case the first group (\[\{) will match first two symbols

    and the second group (.*) the rest of the string