Use ?
for character identity
?a
<<98>> == <<?b>>
<<?g, ?o, ?\n>> == "go
"
Quotes can also be used
<<"a">> == <<?a>>
<<"[", x :: utf8, "]">> = "[a]"
<<x>>
A match group without a known length can only come at the end of a pattern
<<num :: size(16), rest::binary>> = <<1, 0, ?a, ?b, ?c>>
{num, rest}
<<line :: binary, ?\n>> = "this will not work
"
<>
is allowed in matches
"foo" <> rest = "foobar"
rest
binary-size
and unit(8)-size
mean different things
<<x :: binary-size(5)>> = "frail"
x
<<x :: unit(8)-size(5)>> = "frail"
x
Matching length and resusing later in a pattern is useful
<<len :: size(2), msg :: size(len)>> = <<0b1001 :: size(4)>>
msg
# also works in reverse order
<<msg :: size(len), len :: size(2)>> = <<0b1010 :: size(4)>>
msg
# Can only be passed as part of the binary itself
{hlen, blen, <<header :: binary-size(hlen), body :: binary-size(blen)>>} = {4, 4, "headbody"}
# or as a previously bound variable
{hlen, blen} = {4, 4}
<<header :: binary-size(hlen), body :: binary-size(blen)>> = "headbody"
{header, body}