import re regex = r"(\w+(?:\s+\w+)*)\s*,\s*(?:\w+\s+)*(\w+)"s = ("Mr John,Carpenter,Mrs Liza,amazing painter")print(re.findall(regex, s))
Standard input is empty
[('Mr John', 'Carpenter'), ('Mrs Liza', 'painter')]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!