R qdap::mgsub, how to pass a pattern with a regular expression?

孤街醉人 提交于 2019-12-03 18:16:31

问题


In a previous question (replace string in R giving a vector of patterns and vector of replacements) y found that mgsub does have as pattern a string that does not need to br escape. That is good when you want to replace text like '[%.+%]' as a literal string, but then is a bad thing if you need to pass a real regular expression like:

library('stringr')
library('qdap')

tt_ori <- 'I have VAR1 and VAR2'
ttl <- list(ttregex='VAR([12])', val="val-\\1")
ttl 

# OK
stringr::str_replace_all( tt_ori, perl( ttl$ttregex), ttl$val)
# [1] "I have val-1 and val-2"

# OK
mapply(gsub, ttl$ttregex, ttl$val, tt_ori, perl=T)
# [1] "I have val-1 and val-2"

# FAIL
qdap::mgsub(ttl$ttregex, ttl$val, tt_ori)
# [1] "I have VAR1 and VAR2"

How can I pass a regular expression to mgsub?

[UPDATE] @BondeDust is rigth, with this oversimplyfied example the question does not make sense. The reason of wanting to use mgsub is for its ability for using a vector of patterns and a vector of replaces with a single string and make all substitutions in this string.

For example in the next example

> tt_ori <- 'I have VAR1 and VAR2 at CARTESIAN'
> ttl <- list(  ttregex=c('VAR([12])', 'CARTESIAN')
+             , valregex=c("val-\\1", "XY")
+             , tt=c('VAR1', 'VAR2', 'CARTESIAN')
+             , val=c('val-1', 'val-2', 'XY')
+             )
> ttl 
$ttregex
[1] "VAR([12])" "CARTESIAN"
$valregex
[1] "val-\\1" "XY"     

$tt
[1] "VAR1"      "VAR2"      "CARTESIAN"
$val
[1] "val-1" "val-2" "XY"   

# str_replace and gsub return multiple strings with partial substitutions 
> stringr::str_replace_all( tt_ori, perl( ttl$ttregex), ttl$valregex)
[1] "I have val-1 and val-2 at CARTESIAN" "I have VAR1 and VAR2 at XY"         
> mapply(gsub, ttl$ttregex, ttl$valregex, tt_ori, perl=T)
                            VAR([12])                             CARTESIAN 
"I have val-1 and val-2 at CARTESIAN"          "I have VAR1 and VAR2 at XY" 

# qdap (passing regexes) FAIL
> qdap::mgsub(ttl$ttregex, ttl$valregex, tt_ori)
[1] "I have VAR1 and VAR2 at XY"

# qdap (passing strings) is OK
> qdap::mgsub(ttl$tt, ttl$val, tt_ori)
[1] "I have val-1 and val-2 at XY"

I want to take advantage of using regexes when possible and not write all the possible strings (sometimes I don't know them in advance).


回答1:


Change fixed = TRUE to fixed = FALSE



来源:https://stackoverflow.com/questions/28532172/r-qdapmgsub-how-to-pass-a-pattern-with-a-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!