namez <- c("foo2003", "bar2340", "naught45")
patternz <- "03"
grepl("[patternz]$",namez)
It does not work. What should I substitute [patternz] with, so the regular expression will match the contents of the patternz variable?
[edit] Notice that I want to match the string “03”, not the digits “0” and “3” separately.
Must admit to struggling to see what the problem is here. For the example stated nothing more than
is required as
patternzis a character vector and the aim is not to match0&3but to match the literal"03"If you need this to match only at the end of the strings, then we do need to add
"$"either by hand:or via a
paste0()operationThe issue is to use
patternzas the actual regexp and base R functions handle this perfectly.