regex4seq package

Module contents

regex4seq.NONE = This is a singleton that matches the empty sequence.

Represents a pattern that matches no items. Rougly equivalent to regular expressions ‘()’

regex4seq.ANY = This is a singleton that matches any one item from a sequence.

This is a pattern that matches unconditionally against any one item from a sequence. Roughly equivalent to ‘.’ in regular expressions.

regex4seq.MANY = This is a singleton that matches any number of items from a sequence.

This is a pattern that matches unconditionally against any number of items from a sequence. Roughly equivalent to ‘.*’ in regular expressions.

class regex4seq.IfItem(predicateFunction: Callable[[T], bool])

Wraps a predicate function to create a pattern that matches against any item that satisfies the predicate.

regex4seq.IfItems(*args: Callable[[T], bool])

This is a convenience function that given a sequence of predicate functions returns a pattern that match a sequence of items that each satisfy the corresponding predicate.

class regex4seq.Item(item: T)

Wraps an item to create a pattern that matches against that item using normal equals. If you want to use a different predicate, use IfItem.

otherwise(Q: RegEx4Seq[T])

Same as otherwise but includes an optimization to avoid creating a nested Item.

regex4seq.Items(*args: T)

This is a convenience function that returns a pattern that matches the given arguments, one after another.

class regex4seq.MatchGroup(name, original: RegEx4Seq[T], suchthat=None, extract=None)

Creates a named group that can be used to capture a subsequence of the of the input sequence. The captured subsequence can be retrieved by using the matching attribute from the namespace object returned by the match method.

class regex4seq.OneOf(*items: T)

This is a pattern that matches against any one of the supplied items using equals. Very roughly equivalent to ‘[abc]’ in regular expressions.

otherwise(Q: RegEx4Seq[T])

Same as otherwise but includes an optimization to avoid creating a nested OneOf.

class regex4seq.RegEx4Seq

RegEx4Seq is a regular expression pattern that matches against a sequence of items.

findAllMatches(inputSeq: Sequence[T], namespace: bool = True, start=True, end=True) Iterator[bool | SimpleNamespace]

Returns a generator that will find all matches of the pattern in the inputSeq. Each match is returned as a namespace object that contains the bindings that were captured during the match.

matches(inputSeq: Sequence[T], namespace: bool = True, start=True, end=True, history=None) bool | SimpleNamespace

Returns truthy if the pattern matches the inputSeq. If namespace is set to True, then a namespace object is returned that contains the bindings that were captured during the match.

If start is truthy then the pattern is anchored at the start. If end is truthy then the pattern is anchored at the end. If both start and end are truthy then the pattern must match the entire inputSeq.

optional()

Returns a new pattern that matches zero or one occurences of the original pattern P. Analogous to P? in regular expressions.

otherwise(Q: RegEx4Seq[T])

Given a pattern Q, returns a new pattern that matches the original P or Q. It is analogous to P|Q in regular expressions.

repeat()

Returns a new pattern that matches zero or more occurences of the original pattern P. Analogous to P* in regular expressions.

then(Q: RegEx4Seq[T])

Given a pattern Q, returns a new pattern that matches the original P followed by Q. It is analogous to PQ in regular expressions.

thenAny()

Returns a new pattern that matches the original pattern P followed by any item.

thenIfItems(*predicateFunctions: Callable[[T], bool])

Given a series of predicate-functions, returns a new pattern that matches the original pattern P followed by items that satisfies the predicates in turn.

thenItems(*item: T)

Given an item, returns a new pattern that matches the original pattern P followed by series of items.

thenMany()

Returns a new pattern that matches the original pattern P followed by any number of items.

thenOneOf(*items: T)

Returns a new pattern that matches the original pattern P followed by any one of the items.

var(name, suchthat=None, extract=None)

Returns a new pattern that, when it matches, will capture the matched subsequence into an attribute of a namespace object. The name of the attribute is given by the name argument.

Keywords arguments: suchthat - a predicate function that filters the match extract argument - a function that can transform the match