|
| 1 | +#16 { Retos para Programadores } EXPRESIONES REGULARES |
| 2 | + |
| 3 | +# Bibliography reference |
| 4 | +# Professional JavaScript for web developers by Matt Frisbie [Frisbie, Matt] (z-lib.org) |
| 5 | +#Python Notes for Professionals. 800+ pages of professional hints and tricks (GoalKicker.com) (Z-Library) |
| 6 | +# Additionally, I use GPT as a reference and sometimes to correct or generate proper comments. |
| 7 | + |
| 8 | +""" |
| 9 | + * EJERCICIO: |
| 10 | + * Utilizando tu lenguaje, explora el concepto de expresiones regulares, |
| 11 | + * creando una que sea capaz de encontrar y extraer todos los números |
| 12 | + * de un texto. |
| 13 | + * |
| 14 | + * DIFICULTAD EXTRA (opcional): |
| 15 | + * Crea 3 expresiones regulares (a tu criterio) capaces de: |
| 16 | + * - Validar un email. |
| 17 | + * - Validar un número de teléfono. |
| 18 | + * - Validar una url. |
| 19 | + |
| 20 | +""" |
| 21 | + |
| 22 | +import re |
| 23 | +import time |
| 24 | + |
| 25 | +# Short for print() |
| 26 | +log = print |
| 27 | + |
| 28 | +# Simulate the loading of a web page |
| 29 | +log('Retosparaprogramadores #16') |
| 30 | +time.sleep(2) # Simulate a delay before showing the alert |
| 31 | +log('Retosparaprogramadores #16. Please open the Browser Developer Tools.') |
| 32 | + |
| 33 | +# Define a pattern and flags |
| 34 | +pattern0 = 'abc' |
| 35 | +flags = re.IGNORECASE | re.MULTILINE # Global and case-insensitive |
| 36 | + |
| 37 | +# Create a regular expression using the re.compile() function |
| 38 | +regex = re.compile(pattern0, flags) |
| 39 | + |
| 40 | +log(regex) # Output the regex object |
| 41 | + |
| 42 | +text = "time: 08:07:06" |
| 43 | +pattern1 = r'(time(:)?)? ?([01][0-9]|2[0-3])([:/-])([0-5][0-9])\4([0-5][0-9])' |
| 44 | + |
| 45 | +# Explanation of the regular expression: |
| 46 | +# - (time(:)?) : Optionally matches the word "time" followed by an optional colon (:). |
| 47 | +# - ? : Matches an optional space. |
| 48 | +# - ([01][0-9]|2[0-3]) : Matches the hour, allowing values from 00 to 23. |
| 49 | +# - ([:/-]) : Matches one of the characters :, /, or - and captures it for later use. |
| 50 | +# - ([0-5][0-9]) : Matches the minutes (00 to 59). |
| 51 | +# - \4 : References the fourth captured group, which is the separator (it must be the same as used between the hour and the minutes). |
| 52 | +# - ([0-5][0-9]) : Matches the seconds (00 to 59). |
| 53 | + |
| 54 | +matches1 = re.search(pattern1, text) |
| 55 | + |
| 56 | +if matches1: |
| 57 | + log(matches1.start()) # 0 |
| 58 | + log(matches1.string) # time: 08:07:06 |
| 59 | + log(matches1.group(0)) # time: 08:07:06 |
| 60 | + log(matches1.group(1)) # time: |
| 61 | + log(matches1.group(2)) # : |
| 62 | + |
| 63 | +pattern = r'[0-9]' |
| 64 | +matches = re.search(pattern, text) |
| 65 | +if matches: |
| 66 | + log(matches.group()) # '0' |
| 67 | + |
| 68 | +matches3 = re.findall(pattern, text) |
| 69 | +if matches3: |
| 70 | + log(matches3) # ['0', '8', '0', '7', '0', '6'] |
| 71 | + |
| 72 | +if matches3: |
| 73 | + log(matches3[0]) # 0 |
| 74 | + log(matches3[1]) # 8 |
| 75 | + log(matches3[2]) # 0 |
| 76 | + |
| 77 | +pattern2 = r'\d' |
| 78 | +matches2 = re.search(pattern2, text) |
| 79 | +if matches2: |
| 80 | + log(matches2.group()) # '0' |
| 81 | + |
| 82 | +matches4 = re.findall(pattern2, text) |
| 83 | +if matches4: |
| 84 | + log(matches4) # ['0', '8', '0', '7', '0', '6'] |
| 85 | + |
| 86 | +# exec: Returns the first match and can be used in a loop to find all matches. |
| 87 | +# findall: Returns a list with all matches in a more direct and straightforward way. |
| 88 | + |
| 89 | +# Extra Difficulty Exercise: |
| 90 | + |
| 91 | +def validate_tlf(num): |
| 92 | + if not re.match(r'^\d{11}$', str(num)): |
| 93 | + log("Invalid telephone number. Must be a numeric value and have 11 digits.") |
| 94 | + return False |
| 95 | + return True |
| 96 | + |
| 97 | +def validate_email(email): |
| 98 | + email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' |
| 99 | + if not re.match(email_regex, email): |
| 100 | + log("Invalid email address") |
| 101 | + return False |
| 102 | + return True |
| 103 | + |
| 104 | +def validate_url(url): |
| 105 | + url_regex = r'^(https?:\/\/)?([a-zA-Z0-9.-]+)(:[0-9]{1,5})?(\/[^\s]*)?$' |
| 106 | + if not re.match(url_regex, url) or (not url.startswith('http://') and not url.startswith('https://')): |
| 107 | + log("Invalid URL") |
| 108 | + return False |
| 109 | + return True |
| 110 | + |
| 111 | +num1 = 1562737848 |
| 112 | +num2 = 34587452387 |
| 113 | + |
| 114 | + |
| 115 | +url1 = 'http://palnetaneurodiverso.org' |
| 116 | +url2 = 'https://moebius.org' |
| 117 | +url3 = 'something.com' |
| 118 | +url4 = 'gato' |
| 119 | + |
| 120 | +log(validate_tlf(num1)) # Invalid telephone number. Must be a numeric value and have 11 digits. False |
| 121 | +log(f'Is a valid tlf: {validate_tlf(num2)}') # Is a valid tlf: True |
| 122 | +log(f'Is a valid email: {validate_email(email1)}') # Is a valid email: True |
| 123 | +log(f'Is a valid email: {validate_email(email2)}') # Is a valid email: True |
| 124 | +log(f'Is a valid URL: {validate_url(url1)}') # Is a valid URL: True |
| 125 | +log(f'Is a valid URL: {validate_url(url2)}') # Is a valid URL: True |
| 126 | +log(f'Is a valid URL: {validate_url(url3)}') # Invalid URL & Is a valid URL: False |
| 127 | +log(f'Is a valid URL: {validate_url(url4)}') # Invalid URL & Is a valid URL: False |
| 128 | + |
0 commit comments