-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregulars.py
71 lines (55 loc) · 2.31 KB
/
regulars.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
def email(s):
em = re.compile(r'[a-z\d_-]*@[a-z\d_-]+\.[a-z]{2,}')
m = re.findall(em, s)
print(m)
if m:
return m
else:
return 'email is not correct'
def email_regex(s):
em2 = re.compile(
r'(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?: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]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])')
m2 = re.findall(em2, s)
if m2:
return 'm2'
else:
return 'email is not correct'
def phone1(s):
ans = []
phone_number = re.compile(r'(\+?\d+(|\s|\(|\)|-|){0,4}){7,10}')
for m in re.finditer(phone_number, s):
ans.append(m.group())
return ans
def phone2(s):
ans = []
phone_number2 = re.compile(r'(\+?\d\s?-?\s?\(?\s?\)?\s?\s?-?\s?){7,10}')
for m in re.finditer(phone_number2, s):
ans.append(m.group())
return ans
def tab(s):
pattern = re.compile(r'\s,')
text = re.split(pattern, s)
ans = ','.join(map(str, text))
return ans
def reverse(s):
special_words = re.compile(r'([a-zA-Z]+)-([a-zA-Z]+)')
for m in re.finditer(special_words, s):
s = s.replace(m.group(2), m.group(1))
s = s.replace(m.group(1), m.group(2), 1)
return (s)
def change_plus_1(s):
for m in re.findall(r'-?\d+', s):
k = int(m) + 1
s = s.replace(m, str(k))
return s
def kittens(s):
regular = re.compile(r'\bкот\b', flags = re.IGNORECASE)
return len(re.findall(regular, s))
print(email('my new emeil is [email protected] my old email is [email protected]'))
print(phone1('my number is +4913 - (206) - 90, my second number is 94(128)130'))
print(phone2('my number is +4913 - (206) - 90, my second number is 94(128)130'))
print(tab('Это строка , у которой зачем-то написаны два пробела перед запятой'))
print(reverse('Kind-of green-blue grass'))
print(change_plus_1('У меня 2 яблока и -4 банана'))
print(kittens('кот который кота котами КОт Кот '))