maha.rexy#

Subpackages#

Submodules#

Package Contents#

Classes#

Expression

Regex pattern holder.

ExpressionGroup

A group of expressions that match the same dimension. Expressions are evaluated

ExpressionResult

A result of a single expression.

Functions#

optional_non_capturing_group(*patterns)

Returns an optional non capturing group of patterns.

non_capturing_group(*patterns)

Returns a non capturing groups of patterns.

positive_lookbehind(*patterns)

Returns a positive lookbehind pattern.

positive_lookahead(*patterns)

Returns positive lookahead pattern

named_group(name, pattern)

Returns named pattern group

capture_group(*patterns)

Returns a capturing group pattern

optional_non_capturing_group(*patterns)[source]#

Returns an optional non capturing group of patterns.

Parameters

patterns (Expression | str) –

non_capturing_group(*patterns)[source]#

Returns a non capturing groups of patterns.

Parameters

patterns (Expression | str) –

positive_lookbehind(*patterns)[source]#

Returns a positive lookbehind pattern.

Parameters

patterns (Expression | str) –

positive_lookahead(*patterns)[source]#

Returns positive lookahead pattern

Parameters

patterns (Expression | str) –

named_group(name, pattern)[source]#

Returns named pattern group

Parameters
capture_group(*patterns)[source]#

Returns a capturing group pattern

Parameters

patterns (Expression | str) –

class Expression(pattern, pickle=False)#

Regex pattern holder.

Parameters
  • pattern (str) – Regular expression pattern.

  • pickle (bool) – If True, the compiled pattern will be pickled. This is useful to save compilation time for large patterns.

pattern :str#

Regular expersion(s) to match

compile(self)#

Compile the regular expersion.

classmethod from_cache(cls, cache)#

Load an expression from cache.

Parameters

cache (str) – Name of the cache file.

Returns

Expression.

Return type

Expression

search(self, text)#

Search for the pattern in the input text.

Parameters

text (str) – Text to search in.

Returns

Matched object.

Return type

regex.Match

match(self, text)#

Match the pattern in the input text.

Parameters

text (str) – Text to match in.

Returns

Matched object.

Return type

Match[str]

fullmatch(self, text)#

Match the pattern in the input text.

Parameters

text (str) – Text to match in.

Returns

Matched object.

Return type

Match[str]

sub(self, repl, text)#

Replace all occurrences of the pattern in the input text.

Parameters
  • repl (str) – Replacement string.

  • text (str) – Text to replace.

Returns

Text with replaced occurrences.

Return type

str

parse(self, text)#

Extract values from the input text.

Parameters

text (str) – Text to extract the value from.

Yields

ExpressionResult – Extracted value.

Return type

Iterable[maha.rexy.templates.expression_result.ExpressionResult]

class ExpressionGroup(*expressions, smart=False)#

A group of expressions that match the same dimension. Expressions are evaluated in the order they were added.

Parameters
  • *expressions – List of expressions to match. High-priority expressions should be passed first.

  • smart (bool, optional) – Whether to parse the text in a smart way. See smart_parse().

  • expressions (rx.Expression | ExpressionGroup) –

compile_expressions(self)#
add(self, *expression)#

Add an expression to the group.

Parameters
Return type

None

join(self)#

Returns non capturing group of the expressions.

Returns

Non capturing group of the patterns.

Return type

str

get_matched_expression(self, text)#

Returns the expression that fully matches the text.

Parameters

text (str) – Text to match.

Returns

Expression that fully matches the text.

Return type

Expression

parse(self, text)#

Parses the text.

Parameters

text (str) – Text to parse.

Yields

ExpressionResult – Extracted value.

Return type

Iterable[maha.rexy.ExpressionResult]

normal_parse(self, text)#

Parse the input text and return the extracted values.

Parameters

text (str) – Text to parse.

Yields

ExpressionResult – Extracted value.

Return type

Iterable[maha.rexy.ExpressionResult]

smart_parse(self, text)#

Parses the text. If a value matches two or more expressions, only the first expression parses the value, no value is matched more than once. This means high-priority expressions should be added to the group first.

Parameters

text (str) – Text to parse.

Yields

ExpressionResult – Extracted value.

Return type

Iterable[maha.rexy.ExpressionResult]

class ExpressionResult#

A result of a single expression.

start :int#

Start index of the matched text

end :int#

End index of the matched text

value :Any#

Extracted value

expression :maha.rexy.Expression#

The expression that was used to find the value