Safe, Multiple, Simultaneous String Substitution

Designed to enable simultaneous substitution in strings in a safe fashion. Safe means it does not rely on placeholders (which can cause errors in same length matches).

mgsub logo

A safe, multiple, simultaneous string substitution function

Build Status Coverage Status CRAN_Status_Badge CII Best Practices You have a string you want to make substitutions on. You want to make many different substitutions at the same time and you want them done in a safe way. For example, you want to shift each word in "hey, how are you?" to the left by replacing "hey" with "how", "how" with "are, etc. Existing functions either do not support vectorization (e.g. gsub), don't support simultaneous substition (e.g. stringr::str_replace_all) or do the replacements in an unsafe manner (e.g. qdap::mgsub). This is a lightweight, pure R function with no dependencies to avoid package bloat when being used.

Install it!

#Install the latest version from GitHub


Simply pass in a vector of strings to be modified, a vector of patterns to match and a vector of replacements. Then watch as they are safely, simultaneously replaced!


The pattern to match is supplied first and the replacement vector follows.

mgsub::mgsub("hey, how are you?",c("hey","how","are","you"),c("how","are","you","hey"))

Recycling is to make it easy to provide a single replacement (or a pattern of replacements) for multiple matches.

mgsub::mgsub("hey, ho, let's go!",c("hey","ho","go"),"ugh",recycle=TRUE)

Matches and replacements can still be supplied as regex exressions. Additional arguments can be passed to the sub/gsub/gregexpr family of internal functions.

mgsub::mgsub("Dopazamine is not the same as Dopachloride and is still fake.", 
             c("[Dd]opa(.*?mine)","fake"), c("Meta\\1","real"),


mgsub 1.7.1

  • Added RNG Version control to the tests to support future versions of R. There are no functional changes to the way the package runs, this is only to ensure the testing environment continues to work as expected.

mgsub 1.7

  • Added the mgsub_censor function which enables fast, simultaneous censoring of patterns
  • Trimmed unncessary logic and helper functions

mgsub 1.6

  • New method employed leveraging gregexpr to reduce the amount of computation time spend identifying matches which slightly improves performance.
  • Added 'escapes' from the normal processing if there's only one match to replace to further improve performance.

mgsub 1.5

  • Removed NSE evalutaion, deprecating the named list (dictionary) method
  • Added support for vector inputs
  • Added NA handling
  • Slightly improved vectorized performance
  • Updated license (MIT)

mgsub 1.0.1

  • Added checks for named input to mgsub
  • Added mgsub_alt to support alternative function parameterization (name is temporary)

mgsub 1.0

  • Initial release to CRAN

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


1.7.3 by Mark Ewing, 6 months ago

Report a bug at

Browse source code at

Authors: Mark Ewing [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Suggests covr, testthat, knitr, rmarkdown

Imported by ie2misc, textclean.

Suggested by ie2miscdata, jab.adverse.reactions.

See at CRAN