Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import re | ||
from typing import TYPE_CHECKING, Match, Optional | ||
|
||
import yaml | ||
|
||
if TYPE_CHECKING: | ||
from yamlloader.ordereddict.loaders import Loader | ||
from yaml.nodes import ScalarNode | ||
|
||
|
||
class Regexpr(yaml.YAMLObject): | ||
yaml_tag = u'!regexpr' | ||
|
||
def __init__(self, pattern): | ||
self.pattern = pattern | ||
self.re_pattern = re.compile(pattern) | ||
|
||
def matches(self, string): | ||
return self.re_pattern.match(string) | ||
|
||
@classmethod | ||
def from_yaml(cls, loader, node): | ||
def from_yaml(cls, loader: 'Loader', node: 'ScalarNode') -> 'Regexpr': | ||
return Regexpr(node.value) | ||
|
||
def __init__(self, pattern: str) -> None: | ||
self.pattern = pattern | ||
self.re_pattern = re.compile(pattern) | ||
|
||
def __repr__(self): | ||
return '(regex %s)' % self.pattern | ||
|
||
def matches(self, string: str) -> Optional[Match[str]]: | ||
return self.re_pattern.match(string) |