I have a string looking like a
.
I would like to delete everything before the 2nd to last occurrence of the patter === test
, ===
included.
a <- "=== test : {abc}
=== test : {abc}
=== test : {abc}
=== test : {aUs*}
=== dce
=== test : {12abc}
=== abc
=== test : {abc}
=== test : {dfg}"
result <- "test : {abc}
=== test : {dfg}"
I tried:
gsub(".*=== test", "", a)
How to set the index 2nd to last?
Thanks
We can use strsplit
to split by line breaks and pick the last two elements. paste
them together and use sub
to remove the ===
in the beginning:
sub("^=== ", "", paste(tail(strsplit(a, split = "\\n")[[1]], 2), collapse = "\n"))
# [1] "test : {abc}\n=== test : {dfg}"