|
| 1 | +# Clase en vídeo (03/11/22): https://www.twitch.tv/videos/1642512950 |
| 2 | + |
| 3 | +### Regular Expressions ### |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +# match |
| 8 | + |
| 9 | +my_string = "Esta es la lección número 7: Lección llamada Expresiones Regulares" |
| 10 | +my_other_string = "Esta no es la lección número 6: Manejo de ficheros" |
| 11 | + |
| 12 | +match = re.match("Esta es la lección", my_string, re.I) |
| 13 | +print(match) |
| 14 | +start, end = match.span() |
| 15 | +print(my_string[start:end]) |
| 16 | + |
| 17 | +match = re.match("Esta no es la lección", my_other_string) |
| 18 | +#if not(match == None): # Otra forma de comprobar el None |
| 19 | +#if match != None: # Otra forma de comprobar el None |
| 20 | +if match is not None: |
| 21 | + print(match) |
| 22 | + start, end = match.span() |
| 23 | + print(my_other_string[start:end]) |
| 24 | + |
| 25 | +print(re.match("Expresiones Regulares", my_string)) |
| 26 | + |
| 27 | +# search |
| 28 | + |
| 29 | +search = re.search("lección", my_string, re.I) |
| 30 | +print(search) |
| 31 | +start, end = search.span() |
| 32 | +print(my_string[start:end]) |
| 33 | + |
| 34 | +# findall |
| 35 | + |
| 36 | +findall = re.findall("lección", my_string, re.I) |
| 37 | +print(findall) |
| 38 | + |
| 39 | +# split |
| 40 | + |
| 41 | +print(re.split(":", my_string)) |
| 42 | + |
| 43 | +# sub |
| 44 | + |
| 45 | +print(re.sub("[l|L]ección", "LECCIÓN", my_string)) |
| 46 | +print(re.sub("Expresiones Regulares", "RegEx", my_string)) |
0 commit comments