R qdap::mgsub, how to pass a pattern with a regula

2019-01-29 09:17发布

问题:

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



标签: r qdap