-
Notifications
You must be signed in to change notification settings - Fork 0
/
myregex.py
55 lines (45 loc) · 1.76 KB
/
myregex.py
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
47
48
49
50
51
52
53
54
55
import re
# regex_allowed_chars_in_url = r'[0-9a-zA-Z\-_.~!*\'\(\);:@&=+$,\/?%#]+'
regex_url = r'^https?:\/\/[0-9a-zA-Z\-_.~!*\'\(\);:@&=+$,\/?%#]+'
regex_domain = r"(?:[0-9A-Za-z](?:-?[0-9A-Za-z]){1,62})(?:\.[0-9A-Za-z](?:-?[0-9A-Za-z]){1,62})+"
regex_domain_restrictive = r"(?:^|\/|=)((?:[0-9A-Za-z](?:-?[0-9A-Za-z]){1,62})(?:\.[0-9A-Za-z](?:-?[0-9A-Za-z]){1,62})+)(?=\/|$)"
regex_ip = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
regex_ipv6 = r'(?:[0-9a-fA-F]{0,4}:){1,6}(?:(?:[0-9a-fA-F]{0,4}:[0-9a-fA-F]{0,4}:?)|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))'
regex_hash = r'[a-z-A-Z0-9]{32,128}'
regex_email = r'[\w\-\.]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+'
def get_type(target:str):
result = {"type":"", "subtype":""}
match = re.match(regex_ip, target)
if match and match.group() == target:
result["type"] = "ip"
result["subtype"] = "ipv4"
return result
match = re.match(regex_ipv6, target)
if match and match.group() == target:
result["type"] = "ip"
result["subtype"] = "ipv6"
return result
match = re.match(regex_hash, target)
if match and match.group() == target:
result["type"] = "hash"
hash_len = len(match.group())
if hash_len == 32:
result['subtype'] = 'md5'
if hash_len == 40:
result['subtype'] = 'sha1'
if hash_len == 64:
result['subtype'] = 'sha256'
return result
match = re.match(regex_email, target)
if match and match.group() == target:
result["type"] = "email"
return result
match = re.match(regex_domain, target)
if match and match.group() == target:
result["type"] = "domain"
return result
match = re.match(regex_url, target)
if match and match.group() == target:
result["type"] = "url"
return result
return result