import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\n|;|^)\"([^\";\\n]*?)\"([^\";\\n]+?)\"([^\";\\n]*?)\"(\\n|;|$)";
final String string = "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>";
final String subst = "\\1\"\\2\"\"\\3\"\"\\4\"\\5";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + 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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html