Last active
June 8, 2020 21:37
-
-
Save bylearncursos/3e47c098efcbf6d9acc0c64d37986e1d to your computer and use it in GitHub Desktop.
Aprenda RegEx com o Python!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Aprenda mais sobre como usar o Regex com o Python em: | |
# dojo.bylearn.com.br :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dica: use sempre o 'r' antes da string, para usá-la como Raw String | |
# Isto é trata todos os caracteres como literais (ex: o '\' deixa de ser um escape da linguagem) | |
padrao = r"(meu|padrao)*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resultado = re.search(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Venha se tornar um ByLearner!" | |
padrao = r"\bByLearn\w*\b" | |
resultado = re.search(padrao, texto) | |
print(resultado) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Venha se tornar um ByLearner!" | |
padrao = r"\bByLearn\w*\b" | |
resultado = re.search(padrao, texto) | |
if resultado: | |
print("Encontramos o padrão no texto, entre os índices: " + | |
f"{resultado.start()} e {resultado.end()}") | |
else: | |
print("Não encontramos o padrão no texto") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ocorrencias = re.findall(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "O rato roeu a roupa do rei de Roma" | |
padrao = r"rato|roupa|rei" | |
resultado = re.findall(padrao, texto) | |
print(resultado) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ocorrencias = re.finditer(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "O rato roeu a roupa do rei de Roma" | |
padrao = r"rato|roupa|rei" | |
ocorrencias = re.finditer(padrao, texto) | |
for ocorrencia in ocorrencias: | |
print(f"Encontrei: {ocorrencia.group()} entre os índices {ocorrencia.span()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
novo_texto = re.sub(padrao, substituicao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "A roupa está na máquina de lavar roupas" | |
padrao = r"roupa" | |
substituicao = r"caneca" | |
novo_texto = re.sub(padrao, substituicao, texto) | |
print(novo_texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Um dois quatro quatro cinco" | |
padrao = r"quatro" | |
substituicao = r"tres" | |
novo_texto = re.sub(padrao, substituicao, texto, 1) | |
print(novo_texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "O rato roeu a roupa do rei de Roma" | |
padrao = r"[rR](\w*)" | |
substituicao = r"g\1" | |
novo_texto = re.sub(padrao, substituicao, texto) | |
print(novo_texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lista_string_divida = re.split(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Venha se tornar um ByLeaner" | |
padrao = r"\s" | |
lista_string_divida = re.split(padrao, texto) | |
print(lista_string_divida) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex = re.match(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Felipe Cabrera" | |
padrao = r"^\w+\s\w+$" | |
regex = re.match(padrao, texto) | |
if regex: | |
print("Valido") | |
else: | |
print("Inválido") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
re.search(padrao, texto, flags= re.MULTILINE) | |
re.match(padrao, texto, flags= re.IGNORECASE | re.MULTILINE) | |
re.finditer(padrao, texto, flags= re.IGNORECASE | re.MULTILINE | re.DOTALL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "ByLearn ByLeArN BYLEARN Bylearn" | |
padrao = r"bylearn" | |
ocorrencias = re.finditer(padrao, texto, flags=re.IGNORECASE | re.MULTILINE) | |
for ocorrencia in ocorrencias: | |
print(f"Eu encontrei: {ocorrencia}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex = re.compile(padrao) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto1 = "O cão correu atrás do gato que corria atrás do rato" | |
texto2 = "Tom e Jerry é um desenho de um gato e um rato" | |
padrao = r"[gr]ato" | |
regex_gato_rato = re.compile(padrao) | |
resultado_texto1 = regex_gato_rato.findall(texto1) | |
resultado_texto2 = regex_gato_rato.findall(texto2) | |
print(resultado_texto1) | |
print(resultado_texto2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Venha para a ByLearn! Venha se tornar um ByLearner!" | |
padrao = r"\bByLearn\w*\b" | |
resultado = re.search(padrao, texto) | |
if resultado: | |
print("Encontramos o padrão no texto, entre os índices: " + | |
f"{resultado.start()} e {resultado.end()}") | |
else: | |
print("Não encontramos o padrão no texto") | |
# Reparem que apenas uma ocorrência será encontrada. | |
# Este é um caso onde devemos tomar cuidado ao usar o re.search()! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex = re.fullmatch(padrao, texto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
texto = "Felipe Cabrera" | |
padrao = r"\w+\s\w+" | |
regex = re.fullmatch(padrao, texto) | |
if regex: | |
print("Valido") | |
else: | |
print("Inválido") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment