Say I have a lot of strings, some of them are very long:
Aim for the moon. If you miss, you may hit a star. – Clement Stone
Nothing about us without us
I want to have a text wrapper doing this algorithm:
) that around position 25\n
So that text will be replaced to:
Aim for the moon. If you\nmiss, you may hit a star.\n– Clement Stone
Nothing about us without us
Consulting Wrapping Text With Regular Expressions
(.{1,25})( +|$\n?)
$1\n
But this will produce Nothing about us without\nus
, which is not preferable.
Using a Lookahead Construct in a If-Then-Else Conditionals:
(.{1,25})(?(?=(.{1,5}$).*))( +|$\n?)
$1$2\n
It still produce Nothing about us without\nus
, which is not preferable.
If your input is run line-by-line, and there is no newline character in the middle of a line, then you can try this:
(.{1,25}.{1,5}$|.{1,25}(?= ))
$1\n
Then apply this:
\n
\n