Splitting text 1st X characters, 2nd X characters, 3rd, etc

Hey @patricia and @eddy: Here’s a way…

This results in:

image

It should be fairly obvious how this works, but if not, here’s an explanation:

  • We first truncate to roughly desired number of characters (105 in my example).
  • Because the character in that last position (105) may or may not be whitespace, I :trimmed the truncated string to remove any preceding/trailing whitespace. This ensures that last character is not whitespace.
  • Regex operator \s(\w+)$ matches the word at the very end of the string. We replace that by " MORE…" (yes there’s a space before the start of MORE). You could make that whatever you want, of course.

I’m sure this could be improved (like, there’s probably a REGEX that would handle the issue of the end of the string may or may not be whitespace – so perhaps one could get rid of the :trimmed operation), but this was just a quick-and-dirty experiment.

Anyway, solution above will work (and is actually pretty neat!) though it may not be the absolute best solution. I’m sure someone who’s more of a regex nerd could improve this.

-K-

6 Likes