Search code examples
regexreplaceword-wrap

How to do text wrapping without adding newline if the residue is short?


Description

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:

  1. Starting from the beginning of the string, identify the nearest blank character ( ) that around position 25
  2. If the residue is smaller than 5 character-length, then do nothing. If not, replace that blank character with \n
  3. Identify the next nearest blank character in the end of the next 25 characters
  4. Return to 2 until end of line

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

Attempt 1

Consulting Wrapping Text With Regular Expressions

  • Matching pattern: (.{1,25})( +|$\n?)
  • Replacing pattern: $1\n

But this will produce Nothing about us without\nus, which is not preferable.

Attempt 2

Using a Lookahead Construct in a If-Then-Else Conditionals:

  • Matching pattern: (.{1,25})(?(?=(.{1,5}$).*))( +|$\n?)
  • Replacing pattern: $1$2\n

It still produce Nothing about us without\nus, which is not preferable.


Solution

  • 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:

    • Pattern: (.{1,25}.{1,5}$|.{1,25}(?= ))
    • Substitution: $1\n

    Then apply this:

    • Pattern: \n
    • Substitution: \n