Greenery

4.2.2 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

from greenery import parse

# Parse a regular expression string into a Pattern object
re_pattern = parse('a(b|c)*d')

# Check if a string matches the pattern
assert re_pattern.matches('ad')
assert re_pattern.matches('abd')
assert re_pattern.matches('abbcd')
assert not re_pattern.matches('aed')

# Get a set of all possible strings matched by the pattern (if finite)
# For infinite patterns, this will yield indefinitely, so use a limit.
# A 'language' (alphabet) must often be supplied for operations like union/intersection in v4.0.0+

# Example with specific alphabet (not strictly needed for .strings() but good practice)
alphabet = frozenset({'a', 'b', 'c', 'd'})
strings_generator = re_pattern.strings(language=alphabet)

# Get a few strings (it can be an infinite generator)
some_strings = [next(strings_generator) for _ in range(5)]
print(f"Some strings matched by 'a(b|c)*d': {some_strings}")

# Demonstrate intersection of two patterns
pattern1 = parse('a.*b')
pattern2 = parse('axb')

# When working with FSMs, ensure a consistent language/alphabet
alph1 = pattern1.alphabet
alph2 = pattern2.alphabet
common_alphabet = alph1 | alph2

intersection_pattern = (pattern1 & pattern2).reduce(language=common_alphabet)
print(f"Intersection of 'a.*b' and 'axb': {intersection_pattern}")
assert intersection_pattern.matches('axb')

view raw JSON →