Hi Gustavo, I would recommend sticking with tokenizing the expression and binding tokens separately. It's much easier to reason about and manipulate that way. Putting the expression back together is also a common task--even if it's not easy to see at first, it's a good exercise. :-)
This seems to be in relation to your previous posts concerning translating R to Julia--is that right? If so, one thing you might consider is simply making an R parser (akin to https://github.com/jakebolewski/JuliaParser.jl), which creates expressions directly from R code, skipping the string step. Then the compiler can do the substitutions for you. Of course, if your goal is to create Julia translations of the R code that are then editable, this wouldn't be that useful. ;-) Cheers, Kevin On Sat, Jul 19, 2014 at 9:14 AM, Gustavo Lacerda <[email protected]> wrote: > hi, > > Given bindings::Dict, expr::String, I would like to implement variable > bindings. The problem with using `replace` is that variable > identifiers can be substrings of each other. > > e.g. > bindings = { "a" => "1", "ab" => "2"} > expr = "a+ab+b" > > Desired output: "1+2+b" > > If you tokenize expr and bind each token separately, you then have the > problem of putting them back together in the expression. > One solution would be to sort them by length, and do `replace` from > longest to shortest... but I think there must be a better solution. > > Any ideas? > > Gustavo > > -- > Gustavo Lacerda > http://www.optimizelife.com >
