Monday, 15 August 2011

Easy way to find and replace dynamic values ( {{example}} ) via regex in R -



Easy way to find and replace dynamic values ( {{example}} ) via regex in R -

i have dynamic values obtained json of format {{example_value}}. have r code calculates actual value. however, solution have found replace placeholder actual value long , ugly.

does have neat solutions?

example of replacing {{example_value}} 5.5:

> gsub( gsub("\\}","\\\\}",gsub("\\{","\\\\{","{{example_value}}")), 5.5, "{{example_value}}") [1] "5.5"

another illustration explains why wrote nested gsub:

dictionary = "{{example_value}}" > gsub( gsub("\\}","\\\\}",gsub("\\{","\\\\{",dictionary)), 5.5, "{{example_value}}") [1] "5.5"

typically dictionary list contains dynamic values expect replace.

you can utilize this:

gsub("{{example_value}}", "5.5", subject, perl=true);

regex r

No comments:

Post a Comment