Greenery is a Python library designed for the manipulation of regular expressions by converting them into Finite State Machines (FSMs). It enables powerful operations like finding matching strings, determining unions, intersections, and differences between regular expressions. The current version is 4.2.2, and it maintains a relatively active release cadence with several updates per year.
pip install greeneryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a regular expression string into a Greenery Pattern object, check if strings match, and retrieve generated strings. It also shows a basic example of pattern intersection, highlighting the importance of the `language` parameter for FSM operations in newer versions.
Explicitly pass the `language` argument, typically a `frozenset` of characters. For combining FSMs, use `fsm1.alphabet | fsm2.alphabet` to ensure all relevant characters are included. Example: `fsm1.intersection(fsm2, language=fsm1.alphabet | fsm2.alphabet)`.
Always define and pass an explicit `language` (a `frozenset` of characters) that accurately reflects the intended alphabet of your regular expression, particularly for operations like intersection or when generating strings. For example, `language=frozenset('abc')` or `language=frozenset(string.ascii_letters + string.digits)`.Before iterating, check `if my_pattern.is_finite():` to confirm if the language is finite. If not, or if you only need a sample, use `itertools.islice` to limit the number of strings fetched: `import itertools; some_strings = list(itertools.islice(my_pattern.strings(), 100))`.
Update method calls to explicitly include the `language` argument, which should be a `frozenset` of characters. Example: `(pattern1 & pattern2).reduce(language=pattern1.alphabet | pattern2.alphabet)`.
Consult the official `greenery` documentation or GitHub repository for the equivalent modern method. For `matches_any`, the `Pattern.matches()` method is now used for individual string matching.
Ensure that all FSMs involved in an operation (like union or intersection) share a consistent and sufficiently broad `language` (alphabet) that encompasses all characters used by both. Provide this unified `language` explicitly when creating the FSMs or performing the operation.
No dependency data recorded yet.