You can create a dynamic regex out of the var
to match and capture *
s that are inside your variables, and reinsert them back with a backreference in gsub
, and remove all other asterisks:
var <- c("a1","a2","a3","a4")
s = "3*a1 + a1*a2 + 4*a3*a4 + a1*a3"
block = paste(var, collapse="|")
pat = paste0("\\b((?:", block, ")\\*)(?=\\b(?:", block, ")\\b)|\\*")
gsub(pat, "\\1", s, perl=T)
## "3a1 + a1*a2 + 4a3*a4 + a1*a3"
See the IDEONE demo
Here is the regex:
\b((?:a1|a2|a3|a4)\*)(?=\b(?:a1|a2|a3|a4)\b)|\*
Details:
\b
- leading word boundary
((?:a1|a2|a3|a4)\*)
- Group 1 matching
(?:a1|a2|a3|a4)
- either one of your variables
\*
- asterisk
(?=\b(?:a1|a2|a3|a4)\b)
- a lookahead check that there must be one of your variables (otherwise, no match is returned, the *
is matched with the second branch of the alternation)
|
- or
\*
- a "wild" literal asterisk to be removed.