maha.rexy.templates¶
Submodules¶
Package Contents¶
Classes¶
Regex pattern holder. |
|
A group of expressions that match the same dimension. Expressions are evaluated |
|
A result of a single expression. |
- class Expression(pattern, pickle=False)[source]¶
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
- 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
regex.Match
- fullmatch(self, text)¶
Match the pattern in the input
text.- Parameters
text (str) – Text to match in.
- Returns
Matched object.
- Return type
regex.Match
- 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)[source]¶
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 (Union[maha.rexy.Expression, ExpressionGroup]) –
- compile_expressions(self)¶
- add(self, *expression)¶
Add an expression to the group.
- Parameters
*expression – Expressions to add.
expression (maha.rexy.Expression) –
- 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
- 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
textand 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]