forked from mouredev/Hello-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_regular_expressions.py
80 lines (54 loc) · 1.89 KB
/
07_regular_expressions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Clase en vídeo: https://youtu.be/TbcEqkabAWU?t=19762
### Regular Expressions ###
import re
# match
my_string = "Esta es la lección número 7: Lección llamada Expresiones Regulares"
my_other_string = "Esta no es la lección número 6: Manejo de ficheros"
match = re.match("Esta es la lección", my_string, re.I)
print(match)
start, end = match.span()
print(my_string[start:end])
match = re.match("Esta no es la lección", my_other_string)
# if not(match == None): # Otra forma de comprobar el None
# if match != None: # Otra forma de comprobar el None
if match is not None:
print(match)
start, end = match.span()
print(my_other_string[start:end])
print(re.match("Expresiones Regulares", my_string))
# search
search = re.search("lección", my_string, re.I)
print(search)
start, end = search.span()
print(my_string[start:end])
# findall
findall = re.findall("lección", my_string, re.I)
print(findall)
# split
print(re.split(":", my_string))
# sub
print(re.sub("[l|L]ección", "LECCIÓN", my_string))
print(re.sub("Expresiones Regulares", "RegEx", my_string))
# Clase en vídeo (09/11/22): https://www.twitch.tv/videos/1648023317
### Regular Expressions Patterns ###
# Para aprender y validar expresiones regulares: https://regex101.com
pattern = r"[lL]ección"
print(re.findall(pattern, my_string))
pattern = r"[lL]ección|Expresiones"
print(re.findall(pattern, my_string))
pattern = r"[0-9]"
print(re.findall(pattern, my_string))
print(re.search(pattern, my_string))
pattern = r"\d"
print(re.findall(pattern, my_string))
pattern = r"\D"
print(re.findall(pattern, my_string))
pattern = r"[l].*"
print(re.findall(pattern, my_string))
email = "mouredev@mouredev.com"
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z-.]+$"
print(re.match(pattern, email))
print(re.search(pattern, email))
print(re.findall(pattern, email))
email = "mouredev@mouredev.com.mx"
print(re.findall(pattern, email))