Eu sei que posso acessar a posição de um match de um regex em uma string usando os métodos start
, end
e span
. Exemplo de um programa que identifica repetições em um texto:
import re
from colorama import Fore,Style
text='''Dorsey was born and raised in St. Louis, Missouri,[8][9]
the son of Tim and Marcia (née Smith) Dorsey.[10][11][12] He is of English, Irish, and Italian descent.[13]
His father worked for a company that developed mass spectrometers and his mother was a homemaker.[14]
He was raised Catholic, and his uncle is a Catholic Catholic priest in Cincinnati.[15] He attended the Catholic
Bishop DuBourg High School. In his younger days, Dorsey worked occasionally as a fashion model.
[16][17][18][19][20] By age 14, Dorsey had become interested in dispatch dispatch routing. Some of the open-source software he created in the area of dispatch logistics is still used by taxicab companies.[10] Dorsey enrolled at the University of Missouri–Rolla in 1995 and attended for two-plus years[15] before transferring to New York University in 1997, but he dropped out two years later,[21] one semester short of graduating.[15]
He came up with the idea that he developed as Twitter while studying at NYU.[15][22]
'''
print("Searching for repeated words ...", "\n")
try:
result=re.search(r'(\w{3,}\s)\1',text)
start=result.start()
end=result.end()
value=result.group()
print("The word \"{}\" is repeated at: ".format(value.split(' ')[0]),"\n\n")
print(text[start-100:start]+ Fore.RED + text[start:end]+ Style.RESET_ALL+text[end:end+200])
except:
print("No repeated words found")
Retorna:
Note que o problema desse programa é que ele identifica apenas uma ocorrência. Eu imaginei que o método start
retornaria uma lista ou tuple quando existe mais de um match, mas não é isso que acontece.
Como posso acessar a posição de todos os matchs de uma expressão regular em uma string? Por exemplo, a palavra dispatch também se repete no texto, mas não sei como faço para pegar a posição dela.
search
não encontra nada ele retornaNone
, então em vez detry
/except
, bastaria fazerif result: encontrou else: não encontrou
Catholic Catholic
edispatch dispatch
ou todas as palavras repetidas ex:Dorsey