Syntax
Reference for the pattern syntax, a dialect of regular expressions adapted for generating random data.
The grammar of Passgen patterns is inspired by regular expressions, with some simplifications, and some additions that are useful for generating random data.
Overview
| Syntax | Name | Meaning |
|---|---|---|
abc | Literal | Characters are printed as they are. |
[a-z], [abc] | Set | One character, chosen at random from the set. |
(abc|def) | Group | One of the alternatives, chosen at random. |
a{7}, a{4,6} | Repetition | Repeats the previous item a fixed or random number of times. |
a? | Optional | Includes the previous item half of the time. |
\w{name} | Word | A random word from the wordlist name. |
\m{name} | Markov chain | A made-up, pronounceable word based on the wordlist name. |
\p{name} | Preset | What the preset name generates. |
\u{E4} | Unicode escape | The character with the given hexadecimal code point. |
For example, this pattern generates something that looks like an email address:
$ passgen '[a-z0-9.]{3,10}@[a-z]{3,10}.(com|net|org)'
bwjc0@fmo.org
Characters
Any Unicode character can be used in a pattern, and is printed as it is:
$ passgen 'abÄü'
abÄü
Escapes
Characters that have a meaning in the syntax must be escaped with a backslash to be used literally:
$ passgen 'ab\[\]'
ab[]
| Escape | Meaning |
|---|---|
\(, \), \[, \], \{, \}, \|, \? | The character itself, without its meaning in the syntax. |
\\ | A backslash. |
\n, \t, \r | A newline, a tab and a carriage return. |
\a, \b, \e, \f, \v | The control characters bell, backspace, escape, form feed and vertical tab. |
\u{E4} | The character with this Unicode code point, written as up to six hexadecimal digits. This one is ä. |
A Unicode escape is only another way to write a character. A character that has a
meaning in the syntax keeps it: \u{7c} is a pipe, and separates segments just
like | does.
Pattern
A pattern is like a group, but without the enclosing parentheses. It consists of one or more segments, separated by the pipe character |. One of the segments is chosen at random. For a literal pipe character, escape it: \|.
$ passgen 'abc|def'
def
Group
A group is enclosed in parentheses and, like a pattern, consists of segments separated by the pipe character |. One of the segments is chosen at random. Groups are also how you apply a modifier to more than one item. Groups can be nested, up to 1024 deep.
$ passgen '(abc|def){2}'
abcdef
Segment
A segment is a chain of items, each of which can be followed by modifiers. It can start with a multiplier, which makes the segment more likely to be chosen.
An item is one of:
- a single character, such as
a, - a set, such as
[a-z], - a group, such as
(abc|def), - a special, such as
\w{english}or\m{english}.
A segment can be empty, in which case it generates nothing. So (abc|) is either
abc or nothing.
Multipliers
By default, every segment in a group is equally likely to be chosen. A multiplier
is a number in curly braces at the start of a segment. A segment with the
multiplier {3} is three times as likely to be chosen as one without. Here, red
is chosen three times out of four:
$ passgen --amount 6 '({3}red|blue)'
blue
red
red
red
blue
blue
A segment with the multiplier {0} is never chosen.
Set
A set is enclosed in square brackets and consists of one or more ranges. One character from the set is chosen at random. A set cannot be empty.
| Pattern | Output |
|---|---|
[abc] | a, b or c |
[0-3] | 0, 1, 2 or 3 |
[a-zA-Z0-9] | a letter or a digit |
The order of the characters in a set does not matter: [ba] generates the same as
[ab]. A character that is listed more than once is as many times as likely to be
chosen. In [aab], a is twice as likely as b. This also applies to characters
that are part of more than one range.
Inside a set, only the brackets and the dash can be escaped as syntax characters:
\[, \] and \-. The other escapes for characters work here too,
except for those of the remaining syntax characters, which have no special meaning
in a set. Write [(|)], not [\(\|\)].
Range
A range is a start character, optionally followed by a dash and an end character.
Ranges are inclusive: a contains only the character a, and a-c contains a,
b and c. A range includes every character from the first to the last one by
Unicode code point, so the last character must not come before the first one.
A dash that is not part of a range has to be escaped, wherever it is in the set.
Write [a-z\-] for a lowercase letter or a dash. [a-z-] and [-a-z] are
errors.
Modifiers
Modifiers come after an item and change how it is generated. They apply to the
item directly before them, so abc{3} repeats only the c. To apply a modifier to
a whole sequence, put it in a group.
An item can have a repetition and be optional at the same time. a{3}? is either
aaa or nothing. Modifiers of the same kind do not add up: in a{2}{3}, the
second repetition replaces the first, which gives aaa. To repeat a repetition,
use a group, as in (a{2}){3}.
Optional
A question mark makes the item optional: it is included half of the time.
| Pattern | Output |
|---|---|
abc? | abc or ab |
[ab][de]? | ad, ae, bd, be, a or b |
abc(def)? | abc or abcdef |
Repetition
A number in curly braces repeats the item that many times.
| Pattern | Output |
|---|---|
abc{3} | abccc |
[ab]{2} | aa, ab, ba or bb |
(word){2} | wordword |
Two numbers give a minimum and a maximum. The number of repetitions is chosen at random between them, inclusive.
| Pattern | Output |
|---|---|
abc{1,3} | abc, abcc or abccc |
[ab]{1,2} | a, b, aa, ab, ba or bb |
The maximum must not be smaller than the minimum. The minimum can be left out, in
which case it is zero: a{,3} is the same as a{0,3}. The maximum cannot be left
out. Unlike in regular expressions, a{3,} is an error, since there is no
sensible way to choose a random number without an upper limit.
Specials
Specials start with a backslash and a letter, followed by a name in curly braces.
| Pattern | Output |
|---|---|
\w{english} | A random word from the wordlist english. |
\m{english} | A made-up word, generated from a Markov chain of the wordlist english. |
\m{english:2} | The same, with a chain of length 2. The length can be from 1 to 8, and is 3 by default. |
\p{apple1} | What the preset apple1 generates, as if its pattern were in parentheses. |
A preset can be used in the pattern of another preset, but not in its own. A wordlist that was not loaded, or a preset that does not exist, is an error, which Passgen reports before it generates anything.
The Markov chains page explains how made-up words work.
Wordlists are loaded with the --wordlist flag or defined in the
configuration file. In the configuration, a
wordlist can be written as { path = "...", hash = "..." }. Passgen then checks
that the file has this hash, which is any prefix of what b2sum prints for it.
Formal grammar
This is the grammar of the syntax. Text in quotes is written as it is.
pattern → segments
segments → segment ( "|" segment )*
segment → multiplier? item*
multiplier → "{" number "}"
item → ( char | set | group | special ) modifiers
modifiers → ( "?" | repetition )*
repetition → "{" number "}" | "{" number? "," number "}"
group → "(" segments ")"
set → "[" range+ "]"
range → set-char ( "-" set-char )?
special → "\w{" name "}" | "\m{" name ( ":" number )? "}" | "\p{" name "}"
char → any character except \ | ( ) [ { ?
| "\" one of ( ) [ ] { } | ?
| escape
set-char → any character except \ ] -
| "\" one of [ ] -
| escape
escape → "\\" | "\" one of a b e f n r t v | "\u{" hex "}"
A { at the start of a segment begins a multiplier. Anywhere else, it begins a
repetition of the item before it.