The method receives an unformatted string and it needs to put line breaks after ';' or before 'i)' and 'i.', where i - integer. If received a string like '; 1)', then it should add only one line breaker after ';' and before '1)'.
// This is ugly - needs to be refined and refactored
private static String format(String unformattedStr) {
Pattern p1 = Pattern.compile("(\\;\\s*)");
Matcher matcher1 = p1.matcher(unformattedStr);
String formattedStr1;
if (matcher1.find()) {
formattedV1 = matcher1.replaceAll("$1\\\\n");
} else {
formattedV1 = unformattedStr;
}
Pattern p2 = Pattern.compile("(\\d+\\)|\\d+\\.)");
Matcher matcher2 = p2.matcher(formattedV1);
String formattedV2;
if (matcher2.find()) {
formattedV2 = matcher2.replaceAll("\\\\n$1");
}
else {
formattedV2 = formattedV1;
}
String formattedV3 = formattedV2.replaceAll("\\\\n\\s*(\\\\n)+", "\\\\n");
return formattedV3;
}
Patterns,Matchersand formattedStrings. For examplesemiColonPattern,numberingPatternetc. \$\endgroup\$