# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\n|;|^)\"([^\";\n]*?)\"([^\";\n]+?)\"([^\";\n]*?)\"(\n|;|$)"
test_str = ("A stable and simple RegEx, which escapes double-quotes (\") in a CSV file.\n\n"
"## Tests ##\n"
"\"Great thing?!*\";\"I love RegEx!\";\"And especially \"regex101.com\"!\";\"Just amazing...\"\n"
"\"Something090324*?!*\";\"Somethi\"ng090\"324*?!*\";\"Something090324*?!*\";\"Something090324*?!*\"\n"
"Ignore\"THIS\"ItIsNotARealQuote?!*;\"This\"is\"real!\";HereIsNoQuoteSymbolAtAll;This\"IsOnlyOneQuoteSymbol\n\n"
"CSV: Escape double-quotes (\") in quoted \n\n"
"## Issues ##\n"
"- Does not support embedded line breaks, but you can easily modify it to support them. Note that they are currently forbitten to be more stable.\n"
"- Does only allow \" for quoting. (Replace it in the whole RegEx to change this)\n"
"- Only works with ; as a separator. (Replace it in the whole RegEx to change this)\n"
"- Does not allow separator character inside of quotes string. Look at the following example:\n"
"\"Example \"string\"\";\"works\"\n"
"\"Example \"string\";\";\"fails\"\n"
"- Get's problems when the issue happens \"too often\", which means it shows up in cells which are directly side-by-side. You may work around this issue by running it multiple times:\n"
"\"run 1 time \";\"a string with a \"small\" issue\";\"and \"another\" one\"\n"
"\"run 2 times\";\"a string with a \"\"small\"\" issue\";\"and \"another\" one\"\n"
"- It only works with quoted strings (no single \" are escaped; only double ones)\n\n"
"## Changelog ##\n"
"v1.0 - 2016-01-25\n"
"- released\n\n"
"Author: rugk <rugk@posteo.de>")
subst = "\\1\"\\2\"\"\\3\"\"\\4\"\\5"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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