I've always used the \Q...\E construct for this, which seems to generally work well – it quotes the ... part to not have special regex meaning.
On Fri, Sep 19, 2014 at 10:18 AM, Robert Feldt <robert.fe...@gmail.com> wrote: > Ok, since I could not find a ready-made solution I went ahead and the code > below seems to cover most of my use cases. Sharing since might be useful to > others... If something similar is already in Julia base please share... ;) > > Cheers, > > Robert > > const PCRESpecialCharsOutsideCharClasses = ".^\$*+?()[{\\|" > const PCRESpecialCharsInsideCharClasses = "^-]\\" > > function escape(repattern::String) > res = Char[] > state = :outside_char_class > for i in 1:length(repattern) > char = repattern[i] > if state == :outside_char_class && in(char, > PCRESpecialCharsOutsideCharClasses) > push!(res, '\\') > if char == '[' > state = :inside_char_class > end > elseif state == :inside_char_class && in(char, > PCRESpecialCharsInsideCharClasses) > push!(res, '\\') > if char == ']' > state = :outside_char_class > end > end > push!(res, char) > end > join(res, "") > end > > repattern(re::Regex) = re.pattern > repattern(s::String) = escape(s) > > merge_re(parts...) = Regex(join(map((p) -> repattern(p), parts))) > > dynpart = "error(\"a\")" > line = "@check $dynpart" > reparts = ["@check", r"\s+", dynpart] > match(merge_re(reparts...), line) > > dynpart2 = "error(\"[a]\")" > line2 = "@check $dynpart2" > reparts2 = ["@check", r"\s+", dynpart2] > match(merge_re(reparts2...), line2) > >