|
| 1 | +import re |
| 2 | +from typing import TypedDict |
| 3 | + |
| 4 | +""" |
| 5 | + Regular expressions... |
| 6 | +""" |
| 7 | + |
| 8 | +print("Regular expressions...") |
| 9 | + |
| 10 | +TEXT: str = "¡Hola Mundo! Hoy es 15/04/2024. Quedan 263 días para terminar el año 2024." |
| 11 | +numbers: str = "".join(element for element in re.findall(r"[0-9]", TEXT)) |
| 12 | + |
| 13 | +print(f"\n{TEXT = }") |
| 14 | + |
| 15 | +print(f"\nNumbers inside `TEXT` --> {numbers}") |
| 16 | + |
| 17 | +print( |
| 18 | + "\n# ---------------------------------------------------------------------------------- #\n" |
| 19 | +) |
| 20 | + |
| 21 | +""" |
| 22 | + Additional challenge... |
| 23 | +""" |
| 24 | + |
| 25 | +print("Additional challenge...") |
| 26 | + |
| 27 | +RegularExpressions = TypedDict( |
| 28 | + "RegularExpressions", |
| 29 | + { |
| 30 | + "email": str, |
| 31 | + "phone_number": str, |
| 32 | + "url": str, |
| 33 | + }, |
| 34 | +) |
| 35 | + |
| 36 | +regular_expressions: RegularExpressions = { |
| 37 | + "email": r"^[a-zA-Z0-9]*@[a-zA-Z0-9]*\.[a-zA-Z]{2,3}$", |
| 38 | + "phone_number": r"^\+[0-9]{1,4} [0-9]{4} [0-9]{4}$", |
| 39 | + "url": r"^https?://([a-zA-Z0-9]*\.)?[a-zA-Z0-9]*\.[a-zA-Z]{2,3}/?$", |
| 40 | +} |
| 41 | + |
| 42 | +EMAILS: list[str] = [ |
| 43 | + "hozlucas28@gmail.com", |
| 44 | + "hozlucas28@dev.com", |
| 45 | + "hozlucas-28@hotmail.com", |
| 46 | + "hozlucas28@melí.com", |
| 47 | + "hozlucas28@edu.com", |
| 48 | +] |
| 49 | + |
| 50 | +PHONE_NUMBERS: list[str] = [ |
| 51 | + "+12 3456 7890", |
| 52 | + "+1 1234 5890", |
| 53 | + "+1234 1234 5690", |
| 54 | + "+123456789", |
| 55 | + "+123456789 1234 5678", |
| 56 | +] |
| 57 | + |
| 58 | +URLS: list[str] = [ |
| 59 | + "https://www.example.cóm", |
| 60 | + "http://example.com", |
| 61 | + "https://subdomain.example.com", |
| 62 | + "http://www.example.c2.uk", |
| 63 | + "https://www.example.org", |
| 64 | +] |
| 65 | + |
| 66 | +print("\nEmails...") |
| 67 | +for email in EMAILS: |
| 68 | + is_valid: bool = len(re.findall(regular_expressions["email"], email)) > 0 |
| 69 | + print(f"Is '{email}' a valid email? {is_valid}") |
| 70 | + |
| 71 | +print("\nPhone numbers...") |
| 72 | +for phone_number in PHONE_NUMBERS: |
| 73 | + is_valid: bool = ( |
| 74 | + len(re.findall(regular_expressions["phone_number"], phone_number)) > 0 |
| 75 | + ) |
| 76 | + print(f"Is '{phone_number}' a valid phone number? {is_valid}") |
| 77 | + |
| 78 | +print("\nUrls...") |
| 79 | +for url in URLS: |
| 80 | + is_valid: bool = len(re.findall(regular_expressions["url"], url)) > 0 |
| 81 | + print(f"Is '{url}' a valid url? {is_valid}") |
0 commit comments