Match

class file_re.Match(match_str, start, end, matchs_list, matchs_dict)[source]

Bases: object

Represents a single regex match against a file.

A Match mirrors the public surface of re.Match: it exposes span(), start(), end(), group(), groups(), and groupdict(). Non-participating capture groups are represented by None (not the empty string), matching re.Match.

Parameters:
  • match_str (str) – The text of the whole match (group 0).

  • start (int) – Character offset (inclusive) where the match begins in the file.

  • end (int) – Character offset (exclusive) where the match ends in the file.

  • matchs_list (list of (str or None)) – Captured subgroups in order. A group that did not participate in the match is None.

  • matchs_dict (dict of str to (str or None)) – Named captured subgroups. A named group that did not participate in the match is None.

__init__(match_str, start, end, matchs_list, matchs_dict)[source]
Parameters:
span()[source]

Return the character span of the match.

Returns:

A (start, end) pair of character offsets into the file.

Return type:

tuple of (int, int)

start()[source]

Return the starting character offset of the match.

Returns:

Character offset (inclusive) where the match begins.

Return type:

int

end()[source]

Return the ending character offset of the match.

Returns:

Character offset (exclusive) where the match ends.

Return type:

int

group(*args)[source]

Return one or more subgroups of the match.

With no arguments, returns the whole match (group 0). With one integer or string, returns the corresponding subgroup. With multiple arguments, returns a tuple of the corresponding subgroups.

Parameters:

*args (int or str) – Group indices (integers) or group names (strings). Index 0 refers to the whole match.

Returns:

A single group value (None if the group did not participate) when exactly one argument is given, otherwise a tuple of group values. With no arguments, returns the whole match string.

Return type:

str or None or tuple of (str or None)

Raises:
  • IndexError – If an integer index is out of range.

  • KeyError – If a string name does not correspond to a named group.

Examples

>>> m.group()
'error: disk full'
>>> m.group(1)
'disk full'
>>> m.group('level', 'msg')
('error', 'disk full')
groups()[source]

Return all captured subgroups as a tuple.

Returns:

All capturing groups defined by the pattern, in order. Non-participating groups are None.

Return type:

tuple of (str or None)

groupdict()[source]

Return a mapping of named subgroups.

Returns:

Named capturing groups defined by the pattern. A group that did not participate in the match maps to None.

Return type:

dict of str to (str or None)

__repr__()[source]

Return repr(self).

Return type:

str