Term's Format
A Term is the core data type in RegexSolver. It represents a regular language (a set of strings), and can be expressed in two interchangeable forms:
- Regex: a regular expression pattern
- FAIR: a stable binary encoding of an automaton
Both forms describe the same language and can be used everywhere in the API.
The life of a Term
A Term passes through four stages:
- You create one, usually with
Term.regex(...), holding a pattern. - You send it to an operation, alone or alongside others.
- You get one back, often in FAIR rather than regex form, because that is what the operation produced. It is a normal Term and goes straight into the next call.
- You reuse it. Answers about a single Term (cardinality, length, pattern) are stored on it. Asking again on the same object skips the API, so keep the Term rather than rebuilding it.
What a returned Term does not carry is a readable pattern. That is a separate step, covered in Getting the regex pattern below.
Formats
Regex (Regular Expression)
RegexSolver supports pure regular expressions (regular language only), with the following rules:
-
Implicit Anchoring Patterns always match the full string.
abcmatches"abc", but not"xabc"or"abcx". -
No Lookaround Constructs like
(?=...)and(?<=...)are not supported. -
Greedy Quantifiers Only Ungreedy forms (
*?,+?,??) are parsed but treated as greedy. -
Dot Matches Everything
.matches any unicode character, including\n. -
No Features Outside Regular Languages Backreferences like
\1are not supported and will return an error.
Regex parsing is powered by Rust's regex-syntax parser. Some unsupported syntax may parse, but will not influence matching semantics.
FAIR (Fast Internal Automaton Representation)
FAIR is a portable encoding of an automaton:
- Encoded using Z85 (ASCII-safe, compact, padded)
- Length varies with automaton complexity
- Opaque and not editable, but fully reusable
- Skips regex parsing and compilation when used, improving performance
Use FAIR to cache, store, or chain results without reprocessing.
Choosing the response format
By default the engine converts nothing: you get back whatever form the operation naturally produced. Which form that is depends on the operation:
| Operation | Default result |
|---|---|
union, concat, repeat | Regex, when every input Term was a regex. FAIR as soon as one input was FAIR. |
intersection, difference, complement | FAIR, always. These are computed on automata. |
Leaving the default alone is the right choice while you are chaining operations, because it never spends time on a conversion you did not ask for.
Set responseFormat when you want to override that. The examples throughout these docs force regex so their output is printable, which is convenient for learning and worth being deliberate about in real code: on a heavy operation, forcing regex makes the conversion draw on the same time budget as the operation itself. See Heavy Operations.
Force regex output:
Code
Force fair output:
Code
Getting the regex pattern
Regardless of the format of a Term it is possible to get the associated regex pattern:
Code
Visualizing a Term
A Term can also be rendered as a Graphviz DOT description of its automaton. This is a debugging aid: paste the output into any Graphviz renderer to see the states and transitions behind a language that has become hard to read as a pattern.
Code
Matching strings locally
Every Term has a matches method that checks whether a given string belongs to its language. It runs locally, with no API call.
matches needs the regex pattern of the Term. A Term created with regex already has it. A Term created with fair, or returned by an operation in FAIR format, does not have it yet. In that case, call getPattern (or get_pattern) once to resolve and cache the pattern, then matches can be called as many times as you want.
If you call matches on a Term whose pattern is not resolved yet, it throws an error.
Code