This function is a multiple global string replacement wrapper that allows access to multiple methods of specifying matches and replacements.
chr.gsub(pattern, replacement, x, recycle = FALSE, ...)
Return a character vector of the same length and with the same attributes as
x
(after possible coercion to character).
a character vector with character strings to be matched.
a character vector equal in length to pattern
or of
length one which are a replacement for matched patterns.
a character vector where matches and replacements are sought.
logical: if TRUE
, replacement is recycled if lengths differ.
additional arguments to pass to the regexpr
or sub
function.
Mark Ewing
Mark Ewing (2019). mgsub: Safe, Multiple, Simultaneous String Substitution. R package version 1.7.1. https://CRAN.R-project.org/package=mgsub
chr.omit
, chr.trim
# Example 1
string <- c("hey ho, let's go!")
chr.gsub(c("hey", "ho"), c("ho", "hey"), string)
# Example 2
string <- "they don't understand the value of what they seek."
chr.gsub(c("the", "they"), c("a", "we"), string)
# Example 3
string <- c("hey ho, let's go!")
chr.gsub(c("hey", "ho"), "yo", string, recycle = TRUE)
# Example 4
string <- "Dopazamine is not the same as dopachloride or dopastriamine, yet is still fake."
chr.gsub(c("[Dd]opa([^ ]*?mine)","fake"), c("Meta\\1","real"), string)
Run the code above in your browser using DataLab