I need to assemble a Regex dynamically from parts. Is there some existing 
way to escape the strings? Currently I do something like:

function escape_regexp_str(str)
  res = Any[]
  for i in 1:length(str)
    if in(str[i], ['"', '(', ')', '+', '*'])
      push!(res, '\\')
    end
    push!(res, str[i])
  end
  join(res)
end

# We want match(merged_re, "check(\"a\")") to match
parts = ["error(", "\"a\"", ")"]
merged_re = Regex(join(map(escape_regexp_str, parts)))
match(merged_re, "check(\"a\")")

but its brittle and I need to ensure to escape everything in my func. It 
feels like there is a simple solution to this problem already in the base?

Thanks,

Robert

Reply via email to