def get_named_group_index(match: typing.Match, name: str) -> typing.Optional[int]: """Get the index of the named group Args: match (Match): The regex match name (str): The group name Returns: int: The index of the group """ if name in match.groupdict(): span = match.span(name) for i in range(1, len(match.groups()) + 1): if match.span(i) == span: return i return None
def get_named_group_at_index(match: typing.Match, idx: int) -> typing.Optional[str]: """Get the name of the group Args: match (Match): The regex match idx (int): The group index Returns: str: The group name """ if len(match.groups()) >= idx: span = match.span(idx) for group in match.groupdict(): if match.span(group) == span: return group return None
def __init__( self, string: Union[str, MutableSequence[str]], header: bool = False, _type_to_spans: Dict[str, List[List[int]]] = None, _span: int = None, _type: int = None, _match: Match = None, _attrs_match: Match = None, ) -> None: """Initialize the object.""" super().__init__(string, _type_to_spans, _span, _type) self._header = header if _match: string = self.string self._match_cache = _match, string if _attrs_match: self._attrs_match_cache = _attrs_match, string else: cell_start = _match.start() attrs_start, attrs_end = _match.span('attrs') self._attrs_match_cache = ATTRS_MATCH( _match[0], attrs_start - cell_start, attrs_end - cell_start), string else: self._attrs_match_cache = self._match_cache = None, None
def get_named_group_index_dict(match: typing.Match) -> typing.Dict[int, str]: """Get the name/index map of the groups Args: match (Match): The regex match Returns: dict: A mapping of indices to names """ group_idx_to_name = {} for group in match.groupdict(): span = match.span(group) for i in range(1, len(match.groups()) + 1): if match.span(i) == span: group_idx_to_name[i] = group break return group_idx_to_name
def get_named_group_index_list(match: typing.Match) -> typing.List[typing.Optional[str]]: """Get the names of the groups Args: match (Match): The regex match Returns: list: The names of the groups by index """ group_names: typing.List[typing.Optional[str]] = [None] * (len(match.groups()) + 1) for i in range(1, len(match.groups()) + 1): span = match.span(i) for group in match.groupdict(): if match.span(group) == span: group_names[i] = group break return group_names
def _starts_ends_overall( m: Match) -> Tuple[MatchIndexes, MatchIndexes, MatchIndexes]: """ Extracts indices from a match object. Returns (groupstarts, groupends, [overall_start, overall_end]) >>> m = re.match(r'.(.)', 'abc') >>> _starts_ends_overall(m) ([1], [2], [0, 2]) >>> m = re.match(r'.', 'abc') >>> _starts_ends_overall(m) ([], [], [0, 1]) """ overall_start, overall_end = m.span() n_matches = len(m.groups()) spans = [m.span(n) for n in range(1, n_matches + 1)] starts = [span[0] for span in spans] ends = [span[1] for span in spans] return starts, ends, [overall_start, overall_end]
def replace_group(match: typing.Match, idx: int, offset_inner: int) -> str: replace_val = _get_replace_group(match, idx, pat.replace_groups) if replace_val is None: return match.group(idx) context.match = match context.match_cur = match.group(idx) context.color_positions_end_idx = offset + match.start(idx) formatter = Formatter(context=context) replace_val, colorpos = formatter.format_string(replace_val) context.match = None context.match_cur = None offset_color_positions(colorpos, match.start(idx) - offset_inner) update_color_positions(color_positions, colorpos) update_color_positions(context.color_positions, colorpos) replace_ranges.append( (match.span(idx), (match.start(idx) - offset_inner, match.start(idx) - offset_inner + len(replace_val)))) return replace_val
def _graft_match( self, graft: pvproject.Graft, fbytes: bytes, match: Match, offset: int, project: 'pvproject.Project', ) -> Tuple[bytes, int]: """ :param graft: a graft with a non-null :attr:`pvproject.Graft.subst` :return: the substituted fbytes """ subst = graft.subst_resolved(project) if subst is not None: mstart, mend = match.span() new_text = match.expand(subst) head = fbytes[:mstart + offset] tail = fbytes[mend + offset:] fbytes = head + new_text + tail offset += len(new_text) - (mend - mstart) return fbytes, offset
def m_rem(m: Match) -> str: """ Return everything that doesn't match """ return m.string[m.span()[1]:]
def replace(match: Match) -> str: start, end = match.span() if start == 0 or start == (len(match.string) - len(match.group(0))): return "" return replacement