[SOLVED] Add extra paragraphs to variable while keeping the same amount of lines?

I have this variable content (3 lines):
link 1
link 2
link 3

I want the final result to be:
link 1

link 2

link 3

(there's an extra line after "link3" as well)

If I use the Search and Replace and I use \n
I get 2 lines after link 1 and after link 2, but no line at all after link 3

If I use \r
I get 1 line after link 1 and after link 2 as expected, but still no line after link 3

I'm not familiar with RegEx (which is what I Googled to get the \n and \r) so I'm not sure if there's another option?

It is because there is no return after your last line so, no return to seach and replace. Your Variable is like this:

link 1↩
link 2↩
link 3

Depending on how you are getting those lines into the Variable, the simplest thing might be just to append the variable with a single :leftwards_arrow_with_hook: before doing the search and replace.

1 Like

Or just use this action

image

Search and Replace Action.kmactions (608 Bytes)

2 Likes

I'm using the FOR EACH action. Shouldn't it add the return after the last line (link 3) as well?

Thing is, what if I want to add other stuff before and after the original line?
For example, my particular case is something like this
[[[
%Variable%Local__VarName%
]]]
\r

So I would have something like this
[[[
link 1
]]]

[[[
link 2
]]]

[[[
link 3
]]]

(extra line after this last one)

If I use the action you shared, I would have an extra line after link 1, link 2, link 3, which is not what I need to do. You know what I mean?

Can you explain what those mean?
(.)$
and
$1\n
?

Also, check my previous reply where I explain what I'm trying to achieve using "FOR EACH". The lines can't be right after the variable line. I need to replace the line with something else and only then, add the new line at the end

It all depends on how you are actually using that Action to assemble your variable. If you are doing something like the below yes, it would add a return to each line of the variable, including the last line.

Can you upload a simplified Macro showing what you are trying to do?

1 Like

I didn't know about the %Return%. Good to know. Thanks

I guess I know what's happening. Since I'm testing this macro, I am displaying the results in a window. For some reason, that window doesn't add the extra line at the bottom, but when I set the clipboard to the variable and then paste the results into TextEdit, for example, it adds the extra line at the bottom, even when I use \r

So it seems that the issue is related to how the display window shows the results.

(.)$ the last character on the line and saved in a capture group
$1 the character in the capture group
\n the newline character

1 Like

If your master variable is changing by having more lines added or deleted from it, I would suggest not adding your double returns until you need them.

Keep your master variable as simple as possible, just the data on lines or separated by another character and only add in the double returns (using a different variable) at the point you need them in your Macro.

1 Like

Yes, the original variable will always be simple lines. I will always create a new variable with the changes (extra characters, new lines, etc).

I was able to create the macro now.
Appreciate your help

Ok let me see if I understand what you mean when you use them in the Search and Replace action:

If the line is "link 1", the (.)$ refers to the last character, in this case "1".
Then $1\n means "replace the 1 with 1+return" (something like that)?

I would assume that "capture group" is almost like a clipboard, kinda?

You’ve got it.

1 Like

Great! Thanks for sharing this.
Saved that piece of info on my new RegEx document so I can learn a bit more about it

1 Like