MCPcopy
hub / github.com/Textualize/rich / highlight_regex

Method highlight_regex

rich/text.py:593–631  ·  view source on GitHub ↗

Highlight text with a regular expression, where group names are translated to styles. Args: re_highlight (Union[re.Pattern, str]): A regular expression object or string. style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a

(
        self,
        re_highlight: Union[Pattern[str], str],
        style: Optional[Union[GetStyleCallable, StyleType]] = None,
        *,
        style_prefix: str = "",
    )

Source from the content-addressed store, hash-verified

591 self.plain += new_spaces
592
593 def highlight_regex(
594 self,
595 re_highlight: Union[Pattern[str], str],
596 style: Optional[Union[GetStyleCallable, StyleType]] = None,
597 *,
598 style_prefix: str = "",
599 ) -> int:
600 """Highlight text with a regular expression, where group names are
601 translated to styles.
602
603 Args:
604 re_highlight (Union[re.Pattern, str]): A regular expression object or string.
605 style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
606 which accepts the matched text and returns a style. Defaults to None.
607 style_prefix (str, optional): Optional prefix to add to style group names.
608
609 Returns:
610 int: Number of regex matches
611 """
612 count = 0
613 append_span = self._spans.append
614 _Span = Span
615 plain = self.plain
616 if isinstance(re_highlight, str):
617 re_highlight = re.compile(re_highlight)
618 for match in re_highlight.finditer(plain):
619 get_span = match.span
620 if style:
621 start, end = get_span()
622 match_style = style(plain[start:end]) if callable(style) else style
623 if match_style is not None and end > start:
624 append_span(_Span(start, end, match_style))
625
626 count += 1
627 for name in match.groupdict().keys():
628 start, end = get_span(name)
629 if start != -1 and end > start:
630 append_span(_Span(start, end, f"{style_prefix}{name}"))
631 return count
632
633 def highlight_words(
634 self,

Callers 4

test_highlight_regexFunction · 0.95
walk_directoryFunction · 0.95
make_filename_textFunction · 0.95

Calls

no outgoing calls

Tested by 2

test_highlight_regexFunction · 0.76