-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy path_regex.py
More file actions
44 lines (32 loc) · 990 Bytes
/
_regex.py
File metadata and controls
44 lines (32 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import re
from typing import Any
_REGEX_CLASS: Any = None
_REGRESS_ERROR: type[Exception] = Exception
try:
from regress import Regex as _REGEX_CLASS
from regress import RegressError as _REGRESS_ERROR
except ImportError: # pragma: no cover - optional dependency
pass
class ECMARegexSyntaxError(ValueError):
pass
def has_ecma_regex() -> bool:
return _REGEX_CLASS is not None
def is_valid_regex(pattern: str) -> bool:
if _REGEX_CLASS is None:
try:
re.compile(pattern)
except re.error:
return False
return True
try:
_REGEX_CLASS(pattern)
except _REGRESS_ERROR:
return False
return True
def search(pattern: str, instance: str) -> bool:
if _REGEX_CLASS is None:
return re.search(pattern, instance) is not None
try:
return _REGEX_CLASS(pattern).find(instance) is not None
except _REGRESS_ERROR as exc:
raise ECMARegexSyntaxError(str(exc)) from exc