package kata.wordwrap.a20110716; /** * Word Wrap Kata. * Copyright (c) 2011, Peter Kofler, licensed under BSD License. */ public class WordWrapRegex { private static final char NEWLINE = '\n'; public static String wrap(String line, int maxLineLen) { return line.replaceAll("([^ ]{" + maxLineLen + "})" + // force split after exact maxLineLen of non spaces "(?=[^ ])" + // which must be followed by a non space "|" + // or "(?=.{" + maxLineLen + "}.)" + // if we have more than maxLineLen characters here, then "(.{1," + maxLineLen + "})" + // wrap at most after maxLineLen characters " ", // and drop the space afterwards "$1$2" + NEWLINE); } }