Cheatsheet

View Source

Quick lookups. See the Reference if anything here needs more explanation, or the Tutorial for a walkthrough.

Operators

SyntaxMeaning
abcliteral sequence
\xliteral x (needed for parens, brackets, braces, the alternation bar, *, +, ?, .; harmless elsewhere)
.any codepoint from :alphabet
a|b|calternation
(...)grouping (() matches the empty string)
[abc]character class
[a-z]character range
[^abc]negated class (any :alphabet codepoint not listed)
\d0-9
\w0-9, A-Z, a-z, _
\sspace, \t, \n, \r
a*zero or more (unbounded unless capped by :max_repeat)
a+one or more (unbounded unless capped by :max_repeat)
a?zero or one
a{m}exactly m
a{m,}m or more (unbounded unless capped by :max_repeat)
a{m,n}m to n

Not supported

SyntaxWhy
\1, \2, ...backreferences -- no finite string set represents "whatever group 1 matched"
(?=) (?!) (?<=) (?<!)lookarounds -- constrain without consuming, meaningless for generation
^ $anchors -- every generated string already matches the whole pattern; ^/$ are just literal characters here
(?<name>...)named/unnamed captures -- nothing to capture when generating

Literal without escaping

^, -, , need no escaping outside a character class / {m,n} -- they're plain literal characters everywhere else.

Options

OptionDefaultAffects
:max_repeatnone -- unbounded*, +, {m,}
:alphabetprintable ASCII (32..126)., [^...]
:seednone -- fresh randomness each callrandom/2 only

API quick reference

Xeger.compile(pattern, opts \\ [])    #=> {:ok, pattern} | {:error, msg}
Xeger.compile!(pattern, opts \\ [])   #=> pattern | raises ArgumentError
Xeger.stream(pattern_or_compiled, opts \\ [])  #=> Enumerable.t()
Xeger.take(pattern_or_compiled, n, opts \\ []) #=> [binary()]
Xeger.random(pattern_or_compiled, opts \\ [])  #=> binary()
Xeger.matches?(pattern, string)       #=> boolean()

# ~X sigil
~X/pattern/     # random match, same as Xeger.random/1
~X/pattern/c    # compile instead
~X/pattern/s    # stream instead

Common gotchas

  • \n, \r, \t are not interpreted as control characters -- they're just the escaped literal letters n, r, t. Use \s (whitespace shorthand) or the actual control character in your source if you need one.
  • a{3,2} (max less than min) is a compile error, not a silently-empty match.
  • A leading ^ inside [...] always negates the class -- to include a literal leading ^, escape it: [\^abc].
  • Enumeration order is shortlex (shortest first), not lexicographic overall -- don't expect Enum.take/2 output to be alphabetically sorted across different lengths.
  • Without :max_repeat, stream/2 on a pattern with a top-level */+/ {m,} is a genuinely infinite stream -- safe with Enum.take/2, but Enum.to_list/1, Enum.count/1, etc. will never return.
  • random/2 picks unbounded-quantifier repeat counts differently from stream/2/take/3: uniformly over min..max_repeat when :max_repeat is given, otherwise a coin flip per extra repeat (unbounded in principle, geometrically short in practice) -- there's no finite range to draw uniformly from without a cap.
  • random/2 raises ArgumentError for a pattern with no possible matches at all (e.g. a negated class that excludes the whole :alphabet).