Regex interpolation

I would like to match a string exactly (using word boundaries) against an interpolated Regex. Here’s what I have but the output looks wrong to me. Am I doing something wrong?

julia> k="t2"
"t2"

julia> r = Regex("\b$(k)\b")
rt"

julia> match(r, "t2") == nothing
true

Many thanks.

Since you are constructing a string first with "", you need to escape the \b:

julia> r = Regex("\\b$(k)\\b")
r"\bt2\b"

julia> match(r, "t2") == nothing
false

There is an example at the end of the documentation for regex literals.

2 Likes