RegexSolver Python API Client
Homepage | Online Demo | Documentation | Developer Console
RegexSolver is a powerful toolkit for building, combining, and analyzing regular expressions. It is designed for constraint solvers, test generators, and other systems that need advanced regex operations.
Installation
pip install regexsolver
Requirements: Python >= 3.10
Quick Start
- Create an API token in the Developer Console.
- Initialize the client and start working with terms.
Synchronous Usage
The synchronous client provides a simple, blocking API.
from regexsolver import RegexSolverClient, Term
client = RegexSolverClient("REGEXSOLVER_API_TOKEN")
term1 = Term.regex(r"(abc|de|fg){2,}")
term2 = Term.regex(r"de.*")
intersection = client.intersection(term1, term2)
pattern = client.get_pattern(intersection)
print(pattern) # de(abc|de|fg)+
Asynchronous Usage
For non-blocking applications, use the asynchronous client.
import asyncio
from regexsolver import AsyncRegexSolverClient, Term
async def main():
async with AsyncRegexSolverClient("REGEXSOLVER_API_TOKEN") as client:
term1 = Term.regex(r"(abc|de|fg){2,}")
term2 = Term.regex(r"de.*")
intersection = await client.intersection(term1, term2)
pattern = await client.get_pattern(intersection)
print(pattern) # de(abc|de|fg)+
asyncio.run(main())
Key Concepts & Limitations
RegexSolver supports a subset of regular expressions that adhere to the principles of regular languages. Here are the key characteristics and limitations of the regular expressions supported by RegexSolver:
- Anchored Expressions: All regular expressions in RegexSolver are anchored. This means that the expressions are treated as if they start and end at the boundaries of the input text. For example, the expression
abcwill match the string "abc" but not "xabc" or "abcx". - Lookahead/Lookbehind: RegexSolver does not support lookahead (
(?=...)) or lookbehind ((?<=...)) assertions. Using them returns an error. - Pure Regular Expressions: RegexSolver focuses on pure regular expressions as defined in regular language theory. This means features that extend beyond regular languages, such as backreferences (
\1,\2, etc.), are not supported. Any use of backreference would return an error. - Greedy/Ungreedy Quantifiers: The concept of ungreedy (
*?,+?,??) quantifiers is not supported. All quantifiers are treated as greedy. For example,a*ora*?will match the longest possible sequence of "a"s. - Line Feed and Dot: RegexSolver handles all characters the same way. The dot
.matches any Unicode character including line feed (\n). - Empty Regular Expressions: The empty language (matches no string) is represented by constructs like
[](empty character class). This is distinct from the empty string.
Response Formats
The API can handle terms in two formats:
regex: a regular expression patternfair: FAIR (Fast Automaton Internal Representation), a stable, signed format used internally by the engine
By default, the engine returns whatever the operation produces, with no extra conversion. Override with response_format, accepted by the operations that return a term:
from regexsolver import ResponseFormat
term1 = Term.regex(r"abcde")
term2 = Term.regex(r"de")
result = client.union(term1, term2, response_format=ResponseFormat.REGEX)
print(result) # regex=(abc)?de
result = client.union(term1, term2, response_format=ResponseFormat.FAIR)
print(result) # fair=...
If the format does not matter, omit response_format or set it to ResponseFormat.ANY.
Regardless of the format, you can always call get_pattern() to obtain the regex pattern of a term.
Bounding execution time
Set a server-side compute timeout in milliseconds with execution_timeout:
from regexsolver.exceptions import TimeoutExceededError
# Limit the server-side compute time to 100 ms
try:
term1 = Term.regex(r".*ab.*c(de|fg).*dab.*c(de|fg).*ab.*c(de|fg).*dab.*c")
term2 = Term.regex(r".*abc.*")
res = client.difference(term1, term2, execution_timeout=100)
except TimeoutExceededError as error:
print(error) # The API returned the following error: The operation took too much time.
Timeout is best effort. The exact time is not guaranteed.
API Overview
RegexSolverClient and AsyncRegexSolverClient expose the following methods. Every method accepts optional keyword arguments: operations that return a term take response_format, deterministic and execution_timeout, while analyze operations and determinize() take execution_timeout only; the response format is not theirs to choose. generate_strings() additionally takes its ordering, seed, length and charset options as keyword arguments.
Analyze
| Method | Return | Description |
|---|---|---|
client.equivalent(term1, term2, **kwargs) |
bool |
True if term1 and term2 accept exactly the same language. |
client.get_cardinality(term, **kwargs) |
Cardinality |
Returns the number of possible matched strings. |
client.get_dot(term, **kwargs) |
str |
Returns a Graphviz DOT representation of the automaton. |
client.get_length(term, **kwargs) |
Length |
Returns the minimum and maximum length of matched strings. |
client.get_pattern(term, **kwargs) |
str |
Returns a regular expression pattern for the term. |
client.is_empty(term, **kwargs) |
bool |
True if the term matches no string. |
client.is_empty_string(term, **kwargs) |
bool |
True if the term matches only the empty string. |
client.is_total(term, **kwargs) |
bool |
True if the term matches all possible strings. |
client.is_deterministic(term, **kwargs) |
bool |
True if the term's automaton is deterministic. Only a deterministic FAIR guarantees consistent string ordering across paginated generate_strings() calls; call determinize() first if this is False. |
client.subset(term_subset, term_superset, **kwargs) |
bool |
True if every string matched by term_subset is also matched by term_superset. |
Note: For AsyncRegexSolverClient, these methods are coroutines and must be awaited.
Compute
| Method | Return | Description |
|---|---|---|
client.complement(term, **kwargs) |
Term |
Computes the complement of the given term. |
client.concat(term1, term2, ..., **kwargs) |
Term |
Concatenates multiple terms in order. |
client.determinize(term, **kwargs) |
Term |
Computes a deterministic FAIR for the given term, suitable for consistent pagination with generate_strings(). |
client.difference(base_term, excluded_term, **kwargs) |
Term |
Computes the difference base_term - excluded_term. |
client.intersection(term1, term2, ..., **kwargs) |
Term |
Computes the intersection of the given terms. |
client.repeat(term, min_val, max_val, **kwargs) |
Term |
Computes the repetition of the term between min_val and max_val times. |
client.union(term1, term2, ..., **kwargs) |
Term |
Computes the union of the given terms. |
Note: For AsyncRegexSolverClient, these methods are coroutines and must be awaited.
Generate
| Method | Return | Description |
|---|---|---|
client.generate_strings(term, limit, offset, **kwargs) |
List[str] |
Generates up to limit unique strings matched by term, skipping the first offset strings. Keyword arguments control path_order, character_order, seed, min_length, max_length and charset. |
Note: For AsyncRegexSolverClient, this method is a coroutine and must be awaited.
Cross-Language Support
If you want to use this library with other programming languages, we provide:
For more information about how to use the wrappers, you can refer to our guide.
You can also take a look at regexsolver which contains the source code of the engine.
License
This project is licensed under the MIT License.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file regexsolver-1.1.0.tar.gz.
File metadata
- Download URL: regexsolver-1.1.0.tar.gz
- Upload date:
- Size: 64.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
360600ab95aca186acc1f9e6e7ab869b061102cad7a42b36b032c04d1d5ca93f
|
|
| MD5 |
07313b596178e94e43a302f37d7d42bb
|
|
| BLAKE2b-256 |
9bb95f2f440ae4e45afbc86a4af58e4774e2df14dcecead8d68e95af0a0c9a9f
|
Provenance
The following attestation bundles were made for regexsolver-1.1.0.tar.gz:
Publisher:
publish.yml on RegexSolver/regexsolver-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
regexsolver-1.1.0.tar.gz -
Subject digest:
360600ab95aca186acc1f9e6e7ab869b061102cad7a42b36b032c04d1d5ca93f - Sigstore transparency entry: 2385154876
- Sigstore integration time:
-
Permalink:
RegexSolver/regexsolver-python@a5161bba0a6d64160bce1637e18b827d268e4d5d -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/RegexSolver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a5161bba0a6d64160bce1637e18b827d268e4d5d -
Trigger Event:
push
-
Statement type:
File details
Details for the file regexsolver-1.1.0-py3-none-any.whl.
File metadata
- Download URL: regexsolver-1.1.0-py3-none-any.whl
- Upload date:
- Size: 108.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40b70bb6c6ba3a6a1f1f8f144b74c16fcbf95f7e8d7da05d14151604df62ab69
|
|
| MD5 |
3fad16be2b0dd4fd410add1c64bed95e
|
|
| BLAKE2b-256 |
3bca4211a9dd842fda1df303390239fb11f5d47748cb685e98811d7101eb1976
|
Provenance
The following attestation bundles were made for regexsolver-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on RegexSolver/regexsolver-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
regexsolver-1.1.0-py3-none-any.whl -
Subject digest:
40b70bb6c6ba3a6a1f1f8f144b74c16fcbf95f7e8d7da05d14151604df62ab69 - Sigstore transparency entry: 2385154901
- Sigstore integration time:
-
Permalink:
RegexSolver/regexsolver-python@a5161bba0a6d64160bce1637e18b827d268e4d5d -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/RegexSolver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a5161bba0a6d64160bce1637e18b827d268e4d5d -
Trigger Event:
push
-
Statement type: