Match
- class file_re.Match(match_str, start, end, matchs_list, matchs_dict)[source]
Bases:
objectRepresents a single regex match against a file.
A
Matchmirrors the public surface ofre.Match: it exposesspan(),start(),end(),group(),groups(), andgroupdict(). Non-participating capture groups are represented byNone(not the empty string), matchingre.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.
- start()[source]
Return the starting character offset of the match.
- Returns:
Character offset (inclusive) where the match begins.
- Return type:
- end()[source]
Return the ending character offset of the match.
- Returns:
Character offset (exclusive) where the match ends.
- Return type:
- 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
0refers to the whole match.- Returns:
A single group value (
Noneif 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:
- 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')