Necesitaba buscar nombres de usuario que fueran similares entre sí, y lo que Ned Batchelder dijo estaba en creíblemente útil. Sin embargo, me encontré con que tenía la producción más limpia cuando utilicé re.compile para crear mi término de búsqueda re:
pattern = re.compile(r"("+username+".*):(.*?):(.*?):(.*?):(.*)"
matches = re.findall(pattern, lines)
de salida se puede imprimir utilizando la siguiente:
print(matches[1]) # prints one whole matching line (in this case, the first line)
print(matches[1][3]) # prints the fourth character group (established with the parentheses in the regex statement) of the first line.
Se utiliza la concatenación de cadenas –