-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregex.py
More file actions
46 lines (31 loc) · 1.2 KB
/
regex.py
File metadata and controls
46 lines (31 loc) · 1.2 KB
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
45
46
import re
from typing import Final
from project.common.utils.regex_utils import concat, unmatched_group
# time regex
TIME_PATTRN: Final[re.Pattern[str]] = re.compile(r'\d+:\d+')
# email regex
LOCAL_PART_CHARS = r'[\w\-._]'
DOMAIN_CHARS = r'[\w\-._]'
TLD_CHARS = r'[A-Za-z]'
local_part = concat([LOCAL_PART_CHARS], without_grouping=True) + r'+'
domain = concat([DOMAIN_CHARS], without_grouping=True) + r'+'
tld = concat([TLD_CHARS], without_grouping=True) + r'+'
EMAIL_REGEX = local_part + r'@' + domain + r'\.' + tld
EMAIL_PATTERN = re.compile(EMAIL_REGEX)
# url regex
SCHEME = r'https?'
CHARS = r'[\w!?/+\-_~;.,*&@#$%()\[\]]'
url_chars = concat([CHARS], without_grouping=True) + r'+'
HTTP_URL_REGEX = SCHEME + r'://' + url_chars
DATA_SCHEME = r'data:'
MEDIATYPE = r'[\w/+.-]+'
BASE64 = r'base64'
DATA = r'[\w+/=]+'
mediatype_part = unmatched_group(MEDIATYPE) + r'?'
base64_part = unmatched_group(BASE64) + r'?'
data_part = unmatched_group(DATA)
DATA_URL_REGEX = DATA_SCHEME + mediatype_part + r'(?:;' + base64_part + r')?,' + data_part
URL_REGEX = concat([HTTP_URL_REGEX, DATA_URL_REGEX])
HTTP_URL_PATTERN = re.compile(HTTP_URL_REGEX)
DATA_URL_PATTERN = re.compile(DATA_URL_REGEX)
URL_PATTERN = re.compile(URL_REGEX)