I have the following example string with line breaks "\n" and spaces " ":
a <- "\n \n \n \nTEST TEST\n"
I would like to remove spaces (" ") directly following after line breaks ("\n"), but not the spaces after other strings (like "TEST" in my toy example). My desired output is therefore:
"\n\n\n\nTEST TEST\n"
I tried stringr's str_remove_all and str_replace_all but didn't succeed as those seem to have problems in this case with the adjacent occurences of the line breaks. This is the closest I got:
str_replace_all(a, "\n[ ]*\n", "\n\n")
I spent hours on this (probably ridiculously easy) problem, any help is thus highly appreciated!