splitting string expression at multiple delimiters in R
I am trying to parse some math expressions in R, and I would therefore like to split them at multiple delimiters +,-,*,/, -(, +(, ), )+ etc so that I get the list of symbolic variables contained in the expression. so e.g. I would like 2*(x1+x2-3*x3) to return "x1", "x2", "x3" Is there a good way of doing it? Thanks. There's probably a cleaner way of doing this, but does this cover your use case(s)? eqn = "3 + 2*(x1+x2-3*x3 - x1/x3) - 5" vars = unlist(strsplit(eqn, split="[-+*/)( ]|[^x][0-9]+|^[0-9]+")) vars = vars[nchar(vars)>0] # To remove empty strings vars [1] "x1" "x2" "x3" "x1" "x3" If