Search code examples
regexapache-nifi

NiFi ReplaceText Regex replace Newline with CRLF


I have a textfile (GetFile-processor) with Unix-style Newline (\n), but need a Windows-style Carriage-Return+LineFeed CRLF (\r\n) in my FlowFile

My ReplaceText-Processor is working

Search value: ((?<!\x0d)\x0a|\x0d(?!\x0a))
Replacement value: ${literal(''):replaceFirst('','\r')}${literal(''):replaceFirst('','\n')}

Question: my RegEx seems to me very complex Do you know an easier way to convert in a FlowFile the \n to \r\n?

Have a nice weekend

Frank


Solution

  • If your file contains \n only, I see no reason to match \r with the pattern.

    Use

    (?<!\r)\n
    

    Replace with ${literal('\r\n')}.

    EXPLANATION

    --------------------------------------------------------------------------------
      (?<!                     look behind to see if there is not:
    --------------------------------------------------------------------------------
        \r                       '\r' (carriage return)
    --------------------------------------------------------------------------------
      )                        end of look-behind
    --------------------------------------------------------------------------------
      \n                       '\n' (newline)