Seblor Seblor - 7 months ago 10
Java Question

Splitting a string on whitespaces

I'm currently trying to splice a string into a multi-line string.
The regex should select whitespaces which 13 charactersbefore.

The problem is that the 13 character count does not reset after the previous selected whitespace. So after the first 13 characters, the regex select every whitespace.

I'm using the following regex with a

positive lookbehind
of 13 characters:

(?<=.{13})


(there is a whitespace at the end)

You can test the regex here and the following code:

import java.util.ArrayList;
public class HelloWorld{

public static void main(String []args){
String str = "This is a test. The app should break this string in substring on whitespaces after 13 characters";

for (String string : str.split("(?<=.{13}) ")) {
System.out.println(string);
}
}
}


the output of this code is:


This is a test.

The

app

should

break

this

string

in

substring

on

whitespaces

after

13

characters


but it should be:


This is a test.

The app should

break this string

in substring on

whitespaces after

13 characters

Answer

You may actually use a lazy limiting quantifier to match the lines and then replace with $0\n:

.{13,}?[ ]

See the regex demo

IDEONE demo:

String str = "This is a test. The app should break this string in substring on whitespaces after 13 characters"; 
System.out.println(str.replaceAll(".{13,}?[ ]", "$0\n"));

Note that the pattern matches:

  • .{13,}? - any character that is not a newline (if you need to match any character, use DOTALL modifier, though I doubt you need it in the current scenario), 13 times at least, and it can match more characters but up to the first space encountered
  • [ ] - a literal space (a character class is redundant, but it helps visualize the pattern).

The replacement pattern - "$0\n" - is re-inserting the whole matched value (it is stored in Group 0) and appends a newline after it.