Search code examples
c#regexstringreplacestring-length

Use Regex.Replace to split text in strings of a max length without breaking words


I'm trying to split text into lines of specific length (in this case 69 characters or less) without breaking words and using either spaces or "/" as the characters I want to match to be replaced with the newline character I am able to do it with Regex.Split using the following code

var strText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
var lines = Regex.Split(strText, @"(.{1,69})(?:\s|/|$)")
Console.WriteLine(string.Join("\n", Array.FindAll(lines,line => !string.IsNullOrEmpty(line))));

This code gives me the following result (it is the desired one)

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

I think it is possible to get the same result with Regex.Replace but I'm unable to do so. I tried with:

var lines = Regex.Replace(strText, @"(.{1,69})(?:\s|/|$)", "\n");

But it returns 7 breakline characters

Is it possible to replace the space or "/" in order to have 7 lines of 69 characters or less without breaking words? (extra points if one liner code)


Solution

  • You can change the replace part with the captured part to get all the lines correctly

    var lines = Regex.Replace(strText, @"(.{1,69})(?:\s|/|$)", "$1\n");