TExcelTAD
Home › Elixir › Elixir Sigils

Elixir Sigils

A sigil is a special syntax for writing a value from text. It starts with a tilde ~, a letter, delimiters and optional modifiers: ~r/hello/i. Sigils make regular expressions, word lists, dates and strings with many quotes easy to write.

Sigil Produces Example
~s a string (with interpolation) ~s(She said "hi")
~S a string, no interpolation or escapes ~S(#{not interpolated})
~c a charlist ~c"abc"
~w a list of words ~w(red green blue)
~r a regular expression ~r/\d+/
~D, ~T, ~N, ~U Date, Time, NaiveDateTime, UTC DateTime ~D[2026-01-15]

Allowed delimiters: //, ||, "", '', (), [], {}, <>, and heredocs """. Pick the one that doesn't appear in your text.

Strings and word lists

elixir · try it yourself
title = "Elixir"
IO.puts(~s(The "#{title}" guide))
IO.puts(~S(Write #{name} to interpolate, and \n for a newline))

IO.inspect(~w(apple banana cherry))
IO.inspect(~w(draft review published)a)
IO.inspect(~w(a b c)c)
The "Elixir" guide
Write #{name} to interpolate, and \n for a newline
["apple", "banana", "cherry"]
[:draft, :review, :published]
[~c"a", ~c"b", ~c"c"]

The a modifier makes ~w return atoms and c returns charlists.

Regular expressions

~r creates a regex (Perl-compatible, PCRE). Modifiers such as i (case-insensitive), u (Unicode), m (multiline) and x (extended, allows comments) go after the closing delimiter:

elixir · try it yourself
email = ~r/^[\w.+-]+@[\w-]+\.[a-z]{2,}$/i

IO.inspect("ada@example.com" =~ email)
IO.inspect("not an email" =~ email)

log = "2026-03-04 ERROR disk full; 2026-03-05 WARN cpu hot"
IO.inspect(Regex.run(~r/\d{4}-\d{2}-\d{2}/, log))
IO.inspect(Regex.scan(~r/(\d{4})-(\d{2})-(\d{2})/, log, capture: :all_but_first))
IO.inspect(Regex.named_captures(~r/(?<level>[A-Z]+) (?<msg>[a-z ]+)/, log))
IO.inspect(Regex.replace(~r/\d/, "card 4111-1111", "*"))
IO.inspect(String.split("a1b22c333d", ~r/\d+/))
true
false
["2026-03-04"]
[["2026", "03", "04"], ["2026", "03", "05"]]
%{"level" => "ERROR", "msg" => "disk full"}
"card ****-****"
["a", "b", "c", "d"]

=~ is a shortcut that returns true when the regex matches anywhere in the string.

Dates and times

elixir · try it yourself
release = ~D[2026-06-01]
meeting = ~T[14:30:00]
deadline = ~N[2026-06-01 18:00:00]
launch = ~U[2026-06-01 09:00:00Z]

IO.inspect(release)
IO.inspect(Date.add(release, 30))
IO.inspect(Date.day_of_week(release))
IO.inspect(Date.diff(~D[2026-12-25], release))
IO.inspect(Time.add(meeting, 90, :minute))
IO.inspect(NaiveDateTime.to_date(deadline))
IO.inspect(DateTime.to_iso8601(launch))
IO.inspect(Date.compare(release, ~D[2026-01-01]))
~D[2026-06-01]
~D[2026-07-01]
1
207
~T[16:00:00]
~D[2026-06-01]
"2026-06-01T09:00:00Z"
:gt

These sigils build structs (%Date{}, %Time{}, …) and validate them at compile time: ~D[2026-02-30] would not compile. Date.day_of_week/1 returns 1 for Monday through 7 for Sunday. Use Date.compare/2 rather than < and > to compare dates.

Heredoc sigils

Sigils also work with triple-quote delimiters, handy for multi-line text with quotes, or for a regex with comments using the x modifier:

elixir · try it yourself
html = ~s"""
<p class="note">Saved!</p>
"""
IO.write(html)

phone = ~r/
  ^\+?(\d{1,3})   # country code
  [\s-]?(\d{3})   # area
  [\s-]?(\d{4,})$ # number
/x

IO.inspect(Regex.run(phone, "+351 912 345678", capture: :all_but_first))
<p class="note">Saved!</p>
["351", "912", "345678"]

Custom sigils

A sigil is just a call to a function named sigil_<letter>. Define one and import it (or define it in the same module) to use it. import is resolved at compile time, so in a script the importing code goes into a module defined after MySigils. Multi-letter uppercase sigils like ~HTML are also allowed:

elixir · try it yourself
defmodule MySigils do
  # ~p"1,200.50" parses a price into integer cents
  def sigil_p(text, _modifiers) do
    text |> String.replace(",", "") |> String.to_float() |> Kernel.*(100) |> round()
  end
end

defmodule Demo do
  import MySigils

  def run do
    IO.inspect(~p"1,200.50")
    IO.inspect(~p"9.99" + ~p"0.01")
  end
end

Demo.run()
120050
1000

Test yourself

What does ~w(one two)a produce?

  1. ["one", "two"]
  2. [:one, :two]
  3. ~c"one two"
  4. "one two"

What is the difference between ~s and ~S?

  1. ~S is case-insensitive
  2. ~S doesn't interpolate or process escapes
  3. ~s creates a charlist
  4. There is none

How do you check whether a string matches a regex?

  1. string == ~r/.../
  2. string =~ ~r/.../
  3. Regex.equal?
  4. match?(string, regex)