我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用re.Match()。
def _processLength(self, lengthMatch): """ Processes the length definition of a netstring. Extracts and stores in C{self._expectedPayloadSize} the number representing the netstring size. Removes the prefix representing the length specification from C{self._remainingData}. @raise NetstringParseError: if the received netstring does not start with a number or the number is bigger than C{self.MAX_LENGTH}. @param lengthMatch: A regular expression match object matching a netstring length specification @type lengthMatch: C{re.Match} """ endOfNumber = lengthMatch.end(1) startOfData = lengthMatch.end(2) lengthString = self._remainingData[:endOfNumber] # Expect payload plus trailing comma: self._expectedPayloadSize = self._extractLength(lengthString) + 1 self._remainingData = self._remainingData[startOfData:]
def _match_to_dict( self, match: Match, errors: bool = True) -> Dict[str, Any]: """Convert a regular expression Match to a dict of (name, value) for all PathVars. Args: match: A :class:`re.Match`. errors: If True, raise an exception for validation failure, otherwise return None. Returns: A (name, value) dict. Raises: ValueError if any values fail validation. """ return match_to_dict(match, self.path_vars, errors)
def find( self, root: PathLike = None, recursive: bool = False) -> Sequence[PathInst]: """Find all paths in `root` matching this spec. Args: root: Directory in which to begin the search. recursive: Whether to search recursively. Returns: A sequence of PathInst. """ if root is None: root = self.default_search_root() find_results = find( root, self.pattern, path_types=[self.path_type], recursive=recursive, return_matches=True) matches = dict( (path, self._match_to_dict(match, errors=False)) for path, match in cast( Sequence[Tuple[str, Match[str]]], find_results)) return [ path_inst(path, match) for path, match in matches.items() if match is not None]
def unmatched(match): """Return unmatched part of re.Match object.""" start, end = match.span(0) return match.string[:start]+match.string[end:]
def _truncate_float(matchobj, format_str='0.2g'): """Truncate long floats Args: matchobj (re.Match object): contains original float format_str (str): format specifier Returns: returns truncated float """ if matchobj.group(0): return format(float(matchobj.group(0)), format_str)
def unmatched(match): """Return unmatched part of re.Match object.""" start, end = match.span(0) return match.string[:start] + match.string[end:]
def match_to_dict( match: Match, path_vars: Dict[str, PathVar], errors: bool = True) -> Dict[str, Any]: """Convert a regular expression Match to a dict of (name, value) for all PathVars. Args: match: A re.Match. path_vars: A dict of PathVars. errors: If True, raise an exception on validation error, otherwise return None. Returns: A (name, value) dict. Raises: ValueError if any values fail validation. """ match_groups = match.groupdict() try: return dict( (name, var(match_groups.get(name, None))) for name, var in path_vars.items()) except ValueError: if errors: raise else: return None # pylint: disable=no-member
def find( self, root: PathLike = None, path_types: Sequence[PathTypeArg] = 'f', recursive: bool = False) -> Sequence[PathInst]: """Find all paths matching this PathSpec. The search starts in 'root' if it is not None, otherwise it starts in the deepest fixed directory of this PathSpec's DirSpec. Args: root: Directory in which to begin the search. path_types: Types to return -- files ('f'), directories ('d') or both ('fd'). recursive: Whether to search recursively. Returns: A sequence of PathInst. """ if root is None: if self.fixed_dir: root = str(self.dir_spec) else: root = self.dir_spec.default_search_root() files = find( root, self.pattern, path_types=path_types, recursive=recursive, return_matches=True) return [ path_inst(path, match_to_dict(match, self.path_vars)) for path, match in cast(Sequence[Tuple[str, Match[str]]], files)]