import re
regex = re.compile(r"(?>(?:(?:^|,|\r?\n)\s*)(?:\"[^\"\\]*(?:\\[\S\s][^\"\\]*)*\"(?:\s*(?:(?=,|\r?\n)|$))|[^,]*(?:\s*(?:(?=,)|$))))*?(?>(?:^|,|\r?\n)\s*)\K\"([^\"\\]*?(?:\\[\S\s][^\"\\]*?)*,[^\"\\]*(?:\\[\S\s][^\"\\]*)*)\"")
test_str = ("243,\"p s\",\"\",\"\",\"\",\"Smith, Ph.D.\",\"11872\",\"Canada\",\"\",29,,,\"\",\"\",\"\",\"\",\"\",\"\",\"UT\",\"\",,\"\",\"Bic. Gaspe\",,,,\"Dry cliffs\",,\"\",\"\",\"\",\"\",\"47633\",\"\",\"\",\"\",\"\",1938,7,126839\n\n"
"ID,NAME,TITLE,DESCRIPTION,,\n"
"PRO1234,\"JOHN SMITH\",ENGINEER,\"JOHN HAS BEEN WORKING\n\n"
"HARD ON BEING A GOOD\n\n"
"SERVENT.\"\n"
"PRO1235,\"KEITH SMITH\",ENGINEER,\"keith, has been working\n\n"
"hard on being a good\n\n"
"servent.\"\n"
"PRO1235,\"KENNY SMITH\",,\"keith has been working\n\n"
"hard on being a good\n\n"
"servent.\"\n"
"PRO1235,\"RICK SMITH\",,,")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html