import re
regex = re.compile(r"(\d+),([^,]*),([^,]*?)(?:\r?\n|$)")
test_str = ("id, text field1, text field2\n"
"1,some text,another text\n"
"2,some text,another text\n"
"3,some text,another text\n"
"1,some text,another text\n"
"2,some \n"
"text,another text\n"
"3,some text,another text\n"
"1,some text,another text\n"
"2,some \n"
"1999-06-21 text,another text\n"
"3,some text,another text")
subst = "$1, \"$2\", \"$3\"\\n"
result = regex.sub(subst, test_str)
if result:
print(result)
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