Lets say I have the following string vector:
x <- c("this!", "is!", "not my name[!]!", "Understrand[!] Mate!",
"Because!I[!] said so!")
I need a way of replacing the exclamation marks “!” with “!\n” but only if the exclamation mark is not surrounded by square brackets. So the output would look like this:
"this!\n"
"is!\n"
"not my name[!]!\n"
"Understrand[!] Mate!\n"
"Because!\nI[!] said so!\n"
I’ve been playing around and just can’t figure it out.
Many thanks in advance for any help.
Tony B.
Using a perl style regex with a negative look-behind
(?<!pattern)and negative look-ahead(?!pattern)assertion:Edit: @Mareks test cases require a Boolean “or” (“|”):