Skip to main content
Update based on new information from comments
Source Link
yuri
  • 4.6k
  • 3
  • 19
  • 40

It looks like you have three distinct cases, two of which can be covered with one regex as @EmilyL pointed out already.

So unless I am missing something can't you just do the following?

private static String format(String unformattedStr) {
     
    String formatted =return unformattedStr.replaceAll("(;\\s)", "$1\\\\n");
    return formatted.replaceAll("?\\s)(\\d+[).])", "\\\\n$1""$1\\\\n$2");
}

It looks like you have three distinct cases, two of which can be covered with one regex as @EmilyL pointed out already.

So unless I am missing something can't you just do the following?

private static String format(String unformattedStr) {
     
    String formatted = unformattedStr.replaceAll("(;\\s)", "$1\\\\n");
    return formatted.replaceAll("(\\d+[).])", "\\\\n$1");
}

It looks like you have three distinct cases, two of which can be covered with one regex as @EmilyL pointed out already.

So unless I am missing something can't you just do the following?

private static String format(String unformattedStr) {    
    return unformattedStr.replaceAll("(;?\\s)(\\d+[).])", "$1\\\\n$2");
}
Source Link
yuri
  • 4.6k
  • 3
  • 19
  • 40

It looks like you have three distinct cases, two of which can be covered with one regex as @EmilyL pointed out already.

So unless I am missing something can't you just do the following?

private static String format(String unformattedStr) {
    
    String formatted = unformattedStr.replaceAll("(;\\s)", "$1\\\\n");
    return formatted.replaceAll("(\\d+[).])", "\\\\n$1");
}