Search and replace from textfile

Hi

I am trying to make a macro to search and replace in a variable from a text-file.
The idea is to have a text file where search and replace written in and delimited by ===.

See this example:

å===aa
ø===oe
æ===ae
\ntest\n===\ntest

And it almost works. In the search for \ntest\n the newlines are found correctly. But in the replace the newline is inserted into the text as \n
But I would like to be inserted as a newline as well.

Can anybody see what is wrong?

Set Variable to Text Macro (v9.0.5)

Set Variable to Text.kmmacros (6.0 KB)

I'm not sure whether this is a bug or by design, but when using a KM Variable for a RegEx Replace string that contains a backslash, as in "\n", KM treats it simply as those characters, and not a LINEFEED as it normally is in RegEx, and in KM.

So, we have to replace those characters with an actual linefeed.
To do this, insert the indicated Search and Replace Action shown below.

Search For:
\\n

Replace With:
\n

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@peternlewis, bug or by design?

when using a KM Variable for a RegEx Replace string that contains a backslash, as in "\n", KM treats it simply as those characters, and not a LINEFEED as it normally is in RegEx, and in KM.

\n in a Search and Replace action replace sting is treated as a linefeed. So would %0A%.

However, text within a variable that is interpolated into the replace field is itself not processed, so if the replacement is %Variable%replace%, then the replacement value will be whatever is in that variable, not whatever is in that variable after processing tokens and escaped characters.

Potentially, you could use the Filter action with Process Tokens on the replace variable, which would process the \n in to line feeds, but it might also process other things depending on what might be there.

1 Like

Filter with Process Tokens works. And in my tests it also works with \t for tabs.
Thanks for the help.

1 Like