import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?: ([^\";\\r\\n]*) #1 value without quotes or\n"
+ " | \\x20* (\"(?:[^\"]+|\"\")*\") \\x20* #2 value inside quotes\n"
+ ")\n"
+ "(?: (;) #3 values delimiter or\n"
+ " | [\\r\\n]+ # rows delimiter\n"
+ ")";
final String string = "501 ; 8300000000000 ; \";Автономный ;\"\";округ\"\"\n"
+ " \"\"Ненецкий\"\";\";test1\n"
+ " 751;8600800000000; \" Автономный округ \"\"Ханты-Мансийский\"\", Район Советский\" ;\n"
+ " 1755;8700300000000;Автономный округ Чукотский, Район Билибинский\n"
+ " 1725;7501900000000;Край Забайкальский, Район Петровск-Забайкальский\n\n"
+ " ;;\n"
+ " 711;2302100000000;Край Краснодарский, Район Лабинский\n"
+ " 729;2401600000000;Край Красноярский, Район Иланский\n"
+ " 765;2700700000000;Край Хабаровский, Район Вяземский";
final String subst = "\\1\\2\\x01\\3\\x02";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.COMMENTS);
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