Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions students/piatkowska_anna/lesson_08_regex/email_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
Email validator - write email validator
that checks if supplied string
is valid e-mail address
'''
import re


def validate_email(email):
emailRule = re.compile(r'''
^([a-zA-Z_%+-]+ # username
[a-zA-Z0-9_%+-]*
\.?
[a-zA-Z0-9_%+-]*)
(@) # @ symbol
([a-zA-Z0-9.-]+) # domain name
(\.[a-zA-Z]{2,4})$ # dot-something
''', re.VERBOSE)
res = emailRule.findall(email)
if (res != []):
print(email, "is valid")
else:
print(email, "is invalid")


if __name__ == "__main__":
validate_email("[email protected]")
validate_email("jan.kowalskigmail.com")
validate_email("jan.kowalski@gmail")
validate_email("[email protected]")
validate_email("[email protected]")
validate_email("[email protected]")
validate_email("[email protected]")
validate_email("[email protected]")
validate_email("jan [email protected]")
validate_email("[email protected]")
48 changes: 48 additions & 0 deletions students/piatkowska_anna/lesson_08_regex/phone_number_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'''
Phone number validator - write phone number validator
that accepts phone numbers in not separated,
space-separated or hypen-separated 9-digit
format with optional country prefix
'''
import re


def validate_number(number):
phoneRule = re.compile(r'''
([+]?\d{2}|\([+]?\d{2}\))? #country prefix
(\s|-)? #separator
( #parts of 3 digits
\d{3}
(\s|-)?
\d{3}
(\s|-)?
\d{3}
| #parts of 2 digits
\d{3}
(\s|-)?
\d{2}
(\s|-)?
\d{2}
(\s|-)?
\d{2}
)
''', re.VERBOSE)
res = phoneRule.findall(number)
if (res != []):
if (res[0][2] != ""):
print(number, "is valid.")
else:
print(number, "is invalid.")


if __name__ == "__main__":
validate_number("123456789")
validate_number("1234")
validate_number("123-456-789")
validate_number("123 456 789")
validate_number("123 45 67 89")
validate_number("123-45-67-89")
validate_number("+48 123 456 789")
validate_number("(+48)123456789")
validate_number("(+48)1234567 89")
validate_number("(+48)1234567")
25 changes: 25 additions & 0 deletions students/piatkowska_anna/lesson_08_regex/postal_code_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Postal code validator - write postal code validator
that checks if supplied
'''
import re


def validate_postal_code(code):
codeRule = re.compile(r'''
^(\d{2}) # firt two digit
([-]{1}) # hyphen
(\d{3})$ # second trhee digit
''', re.VERBOSE)
res = codeRule.findall(code)
if (res != [] and len(res[0]) == 3):
print(code, "is valid")
else:
print(code, "is not valid")


if __name__ == "__main__":
validate_postal_code("50-382")
validate_postal_code("50 382")
validate_postal_code("50382")
validate_postal_code("alama")
34 changes: 34 additions & 0 deletions students/piatkowska_anna/lesson_08_regex/regex_version_of_strip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
Regex Version of strip()
Write a function that takes a string and does
the same thing as the strip() string method.
If no other arguments are passed other than
the string to strip, then whitespace characters
will be removed from the beginning and end of the string.
Otherwise, the characters specified in the second argument
to the function will be removed from the string.
'''
import re


def regex_strip(original, char_to_remove=""):
if char_to_remove == "":
regex_strip = re.compile(r'^\s*(.*?)\s*$')
mo = regex_strip.findall(original)
if mo != []:
return(mo[0])
else:
reStri = r'^[' + char_to_remove + r']' + \
r'*(.*?)[' + char_to_remove + r']*$'
regex_strip = re.compile(reStri)
mo = regex_strip.findall(original)
if mo != []:
return(mo[0])


if __name__ == "__main__":
print(regex_strip(" ala ma kota "))
print(regex_strip(" ala "))
print(regex_strip(" \t ola \t "))
print(regex_strip("abaceOla nie ma kotaabace", "abce"))
print(regex_strip("abaceOlaniemakotaabace", "abce"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
Strong Password Detection
Write a function that uses regular expressions
to make sure the password string it is passed is
strong. A strong password is defined as one that is
at least eight characters long, contains both uppercase
and lowercase characters, and has at least one digit.
You may need to test the string against multiple regex
patterns to validate its strength.
'''
import re


def is_it_strong_password(password):
passwordLengthRule = re.compile(r'^.{8,}$')
res = passwordLengthRule.findall(password)
if (res != []):
passwordRule = re.compile(
r'^(.*?([a-z]+).*?([A-Z]+).*?([0-9]+).*?)'
r'|(.*?([A-Z]+).*?([a-z]+).*?([0-9]+).*?)'
r'|(.*?([0-9]+).*?([a-z]+).*?([A-Z]+).*?)'
r'|(.*?([0-9]+).*?([A-Z]+).*?([a-z]+).*?)'
r'|(.*?([A-Z]+).*?([0-9]+).*?([a-z]+).*?)'
r'|(.*?([a-z]+).*?([0-9]+).*?([A-Z]+).*?)$')
res = passwordRule.findall(password)
if res != []:
print(password, " is strong")
return True
else:
print(password, " is not strong")
return False
else:
print("Password", password, "is too short.")


if __name__ == "__main__":
is_it_strong_password("abc")
is_it_strong_password("1Aa")
is_it_strong_password("aaaaaaaAaaaa")
is_it_strong_password("aaaaaaa1aaaa")
is_it_strong_password("AAAAAAAAaAAA")
is_it_strong_password("aaAaaaa1aa")
is_it_strong_password("Aaaaa1aaaa")
is_it_strong_password("1aaAaaaaaaa")
is_it_strong_password("aa1Aaaaaaaa")
is_it_strong_password("A1aaaaaaa")
is_it_strong_password("a1AAAAAAAAAAA")