Skip to content

Commit 99d7275

Browse files
committed
feat: #16-Python
Reto #16-Python realizado por mrodara
1 parent 5597fd2 commit 99d7275

File tree

1 file changed

+79
-0
lines changed
  • Roadmap/16 - EXPRESIONES REGULARES/python

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
############################################ EXPRESIONES REGULARES ############################################################
2+
3+
import re # Módulo para tratamiento de expresiones regulares
4+
5+
my_string = "El señor Daniel tiene 3 hijos y 5 esposas, además ha sido agraciado con 1000 €"
6+
my_other_string = "A 4 vecinos de la comunidad en el bloque nro. 2 le han tocado 100.000 $ de la lotería"
7+
8+
def extract_numbers_from_text(text: str) -> list:
9+
10+
pattern = r"\d+" # Busca cualquier número entero
11+
12+
return re.findall(pattern=pattern, string=text)
13+
14+
str_list = [my_string, my_other_string]
15+
16+
for phrase in str_list:
17+
matches = extract_numbers_from_text(text=phrase)
18+
19+
if matches:
20+
print(f"En la frase '{phrase}' se encontraron los siguientes números: {matches}")
21+
22+
23+
############################################## EXTRA ###################################################################################
24+
25+
def validate_email(email: str) -> bool:
26+
27+
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-z]{2,}$"
28+
29+
if re.match(pattern=pattern, string=email):
30+
return True
31+
else:
32+
return False
33+
34+
my_fake_email = "root.srisemipresencial.com"
35+
my_trusted_email = "[email protected]"
36+
37+
print(validate_email(my_fake_email))
38+
print(validate_email(my_trusted_email))
39+
40+
def validate_phone_number(phone: str) -> bool:
41+
42+
pattern = r"^\+?[1-9]\d{1,3}[-.\s]?\d{1,4}[-.\s]?\d{3,4}[-.\s]?\d{3,4}$"
43+
44+
if re.match(pattern=pattern, string=phone):
45+
return True
46+
else:
47+
return False
48+
49+
my_fake_phone = "+1234567890"
50+
my_trusted_phone = "+1 234 567 890"
51+
my_trusted_phone_2 = "+123456789"
52+
53+
54+
print(validate_phone_number(my_fake_phone))
55+
print(validate_phone_number(my_trusted_phone))
56+
print(validate_phone_number(my_trusted_phone_2))
57+
58+
def validate_url(url: str) -> bool:
59+
60+
pattern = r"^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?$"
61+
62+
if re.match(pattern=pattern, string=url):
63+
return True
64+
else:
65+
return False
66+
67+
my_fake_url = "http://www.google"
68+
my_trusted_url = "https://www.google.com"
69+
my_trusted_url_2 = "www.google.com"
70+
my_trusted_url_3 = "http://www.google.com"
71+
72+
print(validate_url(my_fake_url))
73+
print(validate_url(my_trusted_url))
74+
############################################## FIN EXTRA ###################################################################################
75+
76+
77+
78+
79+
############################################ FIN EXPRESIONES REGULARES ############################################################

0 commit comments

Comments
 (0)