def href_sub(x: Pattern) -> str: href = x.groups()[1] if href.startswith("http"): return x.group() if href.startswith("bword://"): href = href[len("bword://"):] return "href=%s" % quoteattr("x-dictionary:d:" + unescape( href, {""": '"'}, ))
def href_sub(x: Pattern) -> str: href = x.groups()[1] if href.startswith("http"): return x.group() href = _href_cleanup(href) return "href=" + quoteattr("x-dictionary:d:" + unescape( href, {""": '"'}, ))
def match_regex(self, regex: Pattern, required: bool = False, meaning: str = "") -> str: """Parse input based on a regular expression . Args: regex: Compiled regular expression object. required: Should the exception be raised on unexpected input? meaning: Meaning of `regex` (for use in error messages). Raises: UnexpectedInput: If no syntactically correct keyword is found. """ mo = regex.match(self.input, self.offset) if mo: self.offset = mo.end() return mo.group() if required: raise UnexpectedInput(self, meaning)
def is_matching_pattern( file_path: str, pattern: Pattern = constants.MODULE_NAME_PATTERN, ) -> bool: r""" Checks whether the file's stem matches the given pattern. >>> is_matching_pattern('some.py') True >>> is_matching_pattern('__init__.py') True >>> is_matching_pattern('MyModule.py') False >>> import re >>> is_matching_pattern('123.py', pattern=re.compile(r'\d{3}')) True """ stem = _get_stem(file_path) return pattern.match(stem) is not None
def test_regex_search_negative_constant(regex: Pattern, string: str): assert isinstance(regex, Pattern) assert not bool(regex.match(string))
def test_regex_search_positive_constant(regex: Pattern, string: str): assert isinstance(regex, Pattern) assert bool(regex.search(string))