0

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).

Community
  • 1
  • 1
Pablo Marin-Garcia
  • 4,151
  • 2
  • 32
  • 50
  • This doesn't make a lot of sense. `mgsub` was designed to take two matching pattern and replacement vectors. What you are requesting was already provided by ordinary `gsub`: `gsub(ttl[[1]], ttl[[2]], tt_ori) # [1] "I have val-1 and val-2"` – IRTFM Feb 15 '15 at 23:50
  • I suspect they want to change `fixed = TRUE` to `fixed = FALSE`. – Tyler Rinker Feb 16 '15 at 00:11
  • @BondedDust I have updated the question (was oversimplyfied). You are right that for my initial example does not make sense to use qdap – Pablo Marin-Garcia Feb 16 '15 at 00:26
  • Thanks @TylerRinker I missed that part. It works OK with `> qdap::mgsub(ttl$ttregex, ttl$valregex, tt_ori,fixed = F) [1] "I have val-1 and val-2 at XY"`, Please put that comment as an answer and I would accept it. – Pablo Marin-Garcia Feb 16 '15 at 00:29

1 Answers1

2

Change fixed = TRUE to fixed = FALSE

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519