Strange sigil behaviour difference between ~r{} and ~r//

I’ve stumbled on some weird sigil behaviour. Is this expected?

The following throws “interesting” compile time errors (‘The “do” at line xx is missing terminator “end”’) a few lines later:

matcher = ~r{^(?<sequence>[[:digit:]]{1,9}) }

The following options don’t give me compiler errors though:

matcher = ~r/^(?<sequence>[[:digit:]]{1,9}) /
matcher = ~r{^(?<sequence>[[:digit:]]+) } # not what I want though!

Is this a compiler bug, or should I always use / with the ~r sigil?

If you use {} as delimiters for the sigil, you need to escape occurences of {} within the sigil.

So ~r{^(?<sequence>[[:digit:]]\{1,9\}) } will work.

The same is true for any other delimiter you choose. You need to escape them within the regex, such that elixir knows wich of them are for the content and which are delimiters.

4 Likes