diff --git a/students/piatkowska_anna/lesson_08_regex/email_validator.py b/students/piatkowska_anna/lesson_08_regex/email_validator.py new file mode 100644 index 000000000..9ba6e54b5 --- /dev/null +++ b/students/piatkowska_anna/lesson_08_regex/email_validator.py @@ -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("jan.kowalski@gmail.com") + validate_email("jan.kowalskigmail.com") + validate_email("jan.kowalski@gmail") + validate_email("jan.1kowalski@gmail.com") + validate_email("1jan.kowalski@gmail.com") + validate_email("1jan...kowalski@gmail.com") + validate_email("jan...kowalski@gmail.com") + validate_email("jankowalski@gmail.com") + validate_email("jan kowalski@gmail.com") + validate_email("jankowalski@poczta.wp.pl") diff --git a/students/piatkowska_anna/lesson_08_regex/phone_number_validator.py b/students/piatkowska_anna/lesson_08_regex/phone_number_validator.py new file mode 100644 index 000000000..97a44b03e --- /dev/null +++ b/students/piatkowska_anna/lesson_08_regex/phone_number_validator.py @@ -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") diff --git a/students/piatkowska_anna/lesson_08_regex/postal_code_validator.py b/students/piatkowska_anna/lesson_08_regex/postal_code_validator.py new file mode 100644 index 000000000..41a329a21 --- /dev/null +++ b/students/piatkowska_anna/lesson_08_regex/postal_code_validator.py @@ -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") diff --git a/students/piatkowska_anna/lesson_08_regex/regex_version_of_strip.py b/students/piatkowska_anna/lesson_08_regex/regex_version_of_strip.py new file mode 100644 index 000000000..177e1f00f --- /dev/null +++ b/students/piatkowska_anna/lesson_08_regex/regex_version_of_strip.py @@ -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")) diff --git a/students/piatkowska_anna/lesson_08_regex/strong_password_detection.py b/students/piatkowska_anna/lesson_08_regex/strong_password_detection.py new file mode 100644 index 000000000..4fbb889dd --- /dev/null +++ b/students/piatkowska_anna/lesson_08_regex/strong_password_detection.py @@ -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")