Repeticiones

Expresiones regulares en Python

Maria Eugenia Inzaugarat

Data Science

Caracteres repetidos

Valida la siguiente cadena:

Expresiones regulares en Python

Caracteres repetidos

Valida la siguiente cadena:

Expresiones regulares en Python

Caracteres repetidos

Valida la siguiente cadena:

Expresiones regulares en Python

Caracteres repetidos

Valida la siguiente cadena:

import re
password = "password1234"
re.search(r"\w\w\w\w\w\w\w\w\d\d\d\d", password)
<re.Match object; span=(0, 12), match='password1234'>
Expresiones regulares en Python

Caracteres repetidos

Valida la siguiente cadena:

import re
password = "password1234"
re.search(r"\w{8}\d{4}", password)
<re.Match object; span=(0, 12), match='password1234'>

Cuantificadores:

Metacarácter que indica al motor de regex cuántas veces debe coincidir el carácter inmediatamente a su izquierda.

Expresiones regulares en Python

Cuantificadores

  • Una o más veces: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"       ", text)
Expresiones regulares en Python

Cuantificadores

  • Una o más veces: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-   ", text)
Expresiones regulares en Python

Cuantificadores

  • Una o más veces: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-\d+", text)
['4-3', '10-04']
Expresiones regulares en Python

Cuantificadores

  • Cero o más veces: *

 

my_string = "The concert was amazing! @ameli!a @joh&&n @mary90"

re.findall(r"@\w+\W*\w+", my_string)
['@ameli!a', '@joh&&n', '@mary90']
Expresiones regulares en Python

Cuantificadores

  • Cero o una vez: ?

 

text = "The color of this image is amazing. However, the colour blue could be brighter."

re.findall(r"colou?r", text)
['color', 'colour']
Expresiones regulares en Python

Cuantificadores

  • Al menos n veces, como máximo m: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"                          ", phone_number)
Expresiones regulares en Python

Cuantificadores

  • Al menos n veces, como máximo m: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-                 ", phone_number)
Expresiones regulares en Python

Cuantificadores

  • Al menos n veces, como máximo m: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-\d{3}-           ", phone_number)
Expresiones regulares en Python

Cuantificadores

  • Al menos n veces, como máximo m: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-\d{3}-\d{2,3}-\d{4,}", phone_number)
['1-966-847-3131', '54-908-42-42424']
Expresiones regulares en Python

Cuantificadores

  • Inmediatamente a la izquierda

    • r"apple+": + aplica a e, no a apple
Expresiones regulares en Python

¡Vamos a practicar!

Expresiones regulares en Python

Preparing Video For Download...