Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
String message = "This is a test message which should span a pretty bi§ag§2 line. If I keep going like this, maybe it will wrap onto the next one. This assumes that this works, it's possible that it doesn't.";
Pattern linePattern = Pattern.compile(".{1,50}\\b");
Pattern badEnd = Pattern.compile("§[0-F]^");
Matcher matcher = linePattern.matcher(message);
String buffer = "";
while (matcher.find()) {
String line = buffer +;
Matcher end = badEnd.matcher(line);
if (end.matches()) {
buffer =;
line = line.substring(line.length() - buffer.length());
} else {
buffer = "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment