Quick lookups. See the Reference if anything here needs more
explanation, or the Tutorial for a walkthrough.
Operators
Syntax
Meaning
abc
literal sequence
\x
literal x (needed for parens, brackets, braces, the alternation bar, *, +, ?, .; harmless elsewhere)
.
any codepoint from :alphabet
a|b|c
alternation
(...)
grouping (() matches the empty string)
[abc]
character class
[a-z]
character range
[^abc]
negated class (any :alphabet codepoint not listed)
\d
0-9
\w
0-9, A-Z, a-z, _
\s
space, \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
Syntax
Why
\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
Option
Default
Affects
:max_repeat
none -- unbounded
*, +, {m,}
:alphabet
printable ASCII (32..126)
., [^...]
:seed
none -- fresh randomness each call
random/2 only
API quick reference
Xeger.compile(pattern,opts\\[])#=> {:ok, pattern} | {:error, msg}Xeger.compile!(pattern,opts\\[])#=> pattern | raises ArgumentErrorXeger.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).