regex - R: how to convert part of a string to variable name and return its value in the same string? -
suppose have string marco <- 'polo'. there way can embed marco in middle of string, e.g. x <- 'john plays water marco.' , have x return 'john plays water polo.'?
edit
the solution david kindly offered work hypothetical problem posted above, trying this:
data <- c('kek','koki','ukak','ikka')
v <- c('a|e|i|o|u')
rather deleting vowels, solution can manage (gsub(v,'',data)), how specify, say, vowels between 2 k's? gsub('kvk','',data) doesn't work. appreciated.
if want all vowels between 2 "k" letters removed, propose following:
v <- '[aeiou]' data <- c('kek', 'koki', 'ukak', 'ikka', 'keeuiokaeioukaeiousk') gsub(paste0('(?:\\g(?!^)|[^k]*k(?=[^k]+k))\\k', v), '', data, perl=t) # [1] "kk" "kki" "ukk" "ikka" "kkksk" the \g feature anchor can match @ 1 of 2 positions; start of string position or position @ end of last match. \k resets starting point of reported match , consumed characters no longer included similar lookbehind.
Comments
Post a Comment