reagex

reagex.reagex(pattern, **group_patterns)[source]

Utility function for writing regular expressions with many capturing groups in a readable, clean and hierarchical way. It is just a wrapper of str.format and it works in the same way. A minimal example:

pattern = reagex(
    '{name} "{nickname}" {surname}',
    name='[A-Z][a-z]+',
    nickname='[a-z]+',
    surname='[A-Z][a-z]+'
)
Parameters:
  • pattern (str) – a pattern where you can use str.format syntax for groups {group_name}. Groups are capturing unless they starts with '_'. For each group in this argument, this function expects a keyword argument with the same name containing the pattern for the group.
  • **group_patterns (str) – patterns associated to groups; for each group in pattern of the kind {group_name} this function expects a keyword argument.
Returns:

a pattern you can pass to re functions

reagex.repeated(pattern, sep, least=1, most=None)[source]

Returns a pattern that matches a sequence of strings that match pattern separated by strings that match sep.

For example, for matching a sequence of '{key}={value}' pairs separated by '&', where key and value contains only lowercase letters:

repeated('[a-z]+=[a-z]+', '&') == '[a-z]+=[a-z]+(?:&[a-z]+=[a-z]+)*'
Parameters:
  • pattern (str) – a pattern
  • sep (str) – a pattern for the separator (usually just a character/string)
  • least (int, positive) – minimum number of strings matching pattern; must be positive
  • most (Optional[int]) – maximum number of strings matching pattern; must be greater or equal to least
Returns:

a pattern