Skip to content

Commit 0b474e4

Browse files
committed
RegEx
1 parent e5db994 commit 0b474e4

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

RegEx/check_phone_number.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
3+
"""Ukrainian phone numbers starts with 380 and contain 8 other digits. The task is to create a function
4+
which will tell (return) you if the number is valid. Also I thought that stored boolean data and phone
5+
numbers should be represented as {} - dictionary."""
6+
7+
# old function without using re
8+
def check_phone_number_old(n:list):
9+
res = {}
10+
for el in n:
11+
if el[0] == "+" or el[0] == "-":
12+
el = el[1:]
13+
if len(el) == 12 and el[0:3] == "380" and el.isnumeric() == True:
14+
res[el] = True
15+
else:
16+
res[el] = False
17+
return res
18+
19+
# new function with implementing findall() function
20+
def check_phone_number(n:list):
21+
res = {}
22+
for el in n:
23+
if el[0] == "+" or el[0] == "-":
24+
el = el[1:]
25+
if re.findall(r"380[0-9]{9}", el) and len(el) == 12:
26+
res[el] = True
27+
else:
28+
res[el] = False
29+
return res
30+
31+
if __name__ == "__main__":
32+
print("test 1: ")
33+
print(check_phone_number_old(["+380717083891", "38000000", "22219993152"]), \
34+
check_phone_number(["380717283891", "38000000", "22219993152"]), sep = "\n") # True False False
35+
print("test 2: ")
36+
print(check_phone_number_old(["380090990s", "00000000000", "380380380383"]), \
37+
check_phone_number(["380090990s", "00000000000", "-380380380383"]), sep = "\n") # False, False, True

RegEx/example_1.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
3+
""" with re.findall() method you can loop through string variable as second function argument to find and output
4+
the needed variables, written as first argument.
5+
"""
6+
7+
text_1 = "He spelled some compliments about her #00FF00 eye color"
8+
9+
search_1 = re.findall("\\bher\\b", text_1) # r"\bher\b" | - will output spaced chars
10+
#print(search_1)
11+
12+
search_2 = re.findall("me", text_1)
13+
#print(search_2)
14+
15+
search_3 = re.findall("[a-zA-Z]", text_1) # this will output all alphabet chars in text_1
16+
#print(search_3)
17+
18+
search_4 = re.findall(".", text_1) # this will output all text_1 chars with spaces
19+
#print(search_4)
20+
21+
search_5 = re.findall("[^ a-zA-Z]", text_1) # this will output none alphabet and space chars in text_1
22+
#print(search_5)
23+
24+
search_6 = re.findall("[0-9]", text_1) # this will output all the single digits chars in text_1
25+
print(search_6)
26+
27+
search_7 = re.findall("[0-9][0-9]", text_1) # this will output all the double numbers chars in text_1
28+
print(search_7)

RegEx/example_2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import re
2+
3+
""" This file will represent major, minor and other quantificators in re with findall function"""
4+
5+
#major form
6+
text_1 = "Google, Gooogle, Goooooogle"
7+
match_1 = re.findall(r"o{2,5}", text_1)
8+
#print(match_1)
9+
10+
#minor form
11+
match_2 = re.findall(r"o{2,5}?", text_1)
12+
#print(match_2)
13+
14+
15+
"""short forms"""
16+
17+
match_3 = re.findall(r"o{2,}", text_1)
18+
#print(match_3)
19+
20+
match_4 = re.findall(r"Go{,3}gle", text_1)
21+
#print(match_4)
22+
23+
match_5 = re.findall(r"o{2}", text_1)
24+
#print(match_5)
25+
26+
27+
# also \d, \ and some other commands may be used with quants
28+
text_2 = "380631212123"
29+
30+
match_6 = re.findall("380\d{9}", text_2)
31+
print(match_6)

0 commit comments

Comments
 (0)