Sigils & Regular Expressions
17.1 Built-in sigils
~s(say "hi") string, no escapes/interp ~S(\n stays) ~w(a b c) → ["a","b","c"]; ~w(a b)a → atoms ~N[2026-10-25 13:00:07] ~U[2026-10-25 13:00:07Z]
Delimiters: / | " ' ( [ { <. Uppercase versions disable interpolation/escapes.
17.2 Regex
Modifiers: i (case-insensitive), m (multiline), s (dotall), x (extended), u (unicode), f (firstline).
Regex.match?(~r/^\d{3}-\d{4}$/, "555-1234") # true
"hello" =~ ~r/ell/ # true
"hello" =~ "ell" # substring check
Regex.run(~r/(\w+)@(\w+)/, "me@host") # ["me@host", "me", "host"]
Regex.scan(~r/\d+/, "a1b22c333") # [["1"], ["22"], ["333"]]
Regex.named_captures(~r/(?<y>\d{4})-(?<m>\d{2})/, "2026-10") # %{"m" => "10", "y" => "2026"}
Regex.replace(~r/\s+/, "a b", " ")
String.split("a1b2c", ~r/\d/)17.3 Custom sigils
defmodule MySigils do
def sigil_i(string, []), do: String.to_integer(string)
def sigil_i(string, [?n]), do: -String.to_integer(string)
end
import MySigils
~i(42) # 42
~i(42)n # -42
# Multi-letter uppercase sigils allowed since 1.15: def sigil_HTML(...)