目录

mgsub logo

A safe, multiple, simultaneous string substitution function

R-CMD-check Codecov test coverage CII Best Practices

Why do I want this?

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.packages('mgsub')
#Install the latest version from GitHub
#devtools::install_github("bmewing/mgsub")

Usage

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!

mgsub::mgsub(string,pattern=c(),replacement=c(),recycle=FALSE,...)

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 expressions. 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"),ignore.case=F)
关于

用于字符串替换的R语言包,支持多对一和一对多的替换操作

119.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号