fork download
  1. import re
  2.  
  3. preposition_lst = ['DE LO ', 'DE LA ', 'DE LAS ', 'DEL ', 'DELS ', 'DE LES ', 'DO ', 'DA ', 'DOS ', 'DAS', 'DE ']
  4. cases = ["DE BLAS ZAPATA", "MATIAS DE LA MANO", "LOPEZ FERNANDEZ DE VILLAVERDE", "DE MIGUEL DEL CORRAL", "VIDAL DE LA PEÑA", "MONTAVA DEL ARCO", "DOS CASAS VALLE"]
  5.  
  6. preps = r'(?:D(?:AS?|E(?:[ ]L(?:AS?|ES|O)|LS?)?|OS?) )?'
  7. pattern = fr'^({preps}[^\W\d_]+)( {preps}[^\W\d_]+)?( de {preps}[^\W\d_]+)?$'
  8. for case in cases:
  9. m = re.search(pattern, case, re.I) # re.I makes it case insensitive
  10. if m:
  11. print([x.strip() for x in m.groups() if x])
  12. else:
  13. print(f"No match for {case}")
Success #stdin #stdout 0.03s 9528KB
stdin
Standard input is empty
stdout
['DE BLAS', 'ZAPATA']
['MATIAS', 'DE LA MANO']
['LOPEZ', 'FERNANDEZ', 'DE VILLAVERDE']
['DE MIGUEL', 'DEL CORRAL']
['VIDAL', 'DE LA PEÑA']
['MONTAVA', 'DEL ARCO']
['DOS CASAS', 'VALLE']