I have the following string:
string <- c("ABDSFGHIJLKOP")
and list of substrings:
sub <- c("ABDSF", "SFGH", "GHIJLKOP")
I would like to include < and > after each sub match thus getting:
<ABD><SF><GH><GHIJKOP>
I have tried the following code by pattern matching over a list but as soon as ABDSF is matched SFGH is not recognised anymore because of the inclusion of the < > characters. Anybody have a better idea?
library(stringr)
library(dplyr)
library(magrittr)
string <- c("ABDSFGHIJLKOP")
sub <- c("ABDSF", "SFGH", "GHIJLKOP")
for (s in sub){
string %<>% str_replace_all(., s, paste0('<', s,'>'))
}
print(string)
Result: [1] "<ABDSF><GHIJLKOP>"
EDIT: The problem that I have with the above code is that as soon as the < > characters are inserted, after the first string match the second string SFGH is not recognised anymore because the string is now:
<ABDSF>GHIJLKOP.
So I am looking for a way to match the substrings ignoring the <> characters.