Returning new line after 32 characters + space in PowerShell strings

Viewed 82

I'm looking to run through long string sentences in PowerShell and return a new line after every 32 characters + space (to avoid adding new lines in the middle of a word). Here's what I've tried thus far:

$Runonsentence = 'Interestingly, Bryan A. Garners "The Oxford Dictionary of American Usage and Style" states that while there is a distinction between run-on sentences and comma splices, it isnt typically noteworthy. However, Garner also adds "The distinction can be helpful in differentiating between the wholly unacceptable (true run-on sentences) and the usually-but-not-always unacceptable (comma splices).'

Filter Get-Stringy {
  for($num = 0; $num -le $_.Length-32; $num+=32) {
    $indexer = $_.Substring($num, 32)
    $parser = $indexer.LastIndexOf(" ", 32)
    $trimmer = $indexer.Split($parser)
    $trimmer.Trim()
  }
}

$Runonsentence | Get-Stringy

And here is what it returns:

Interestingly, Bryan A. Garners
"The Oxford Dictionary of Americ
an Usage and Style" states that
while there is a distinction bet
ween run-on sentences and comma
splices, it isnt typically notew
orthy. However, Garner also adds
"The distinction can be helpful
in differentiating between the
wholly unacceptable (true run-on
sentences) and the usually-but-
not-always unacceptable (comma s

As you can probably see from the results above, the words at the end are being cut-off and the entire string isn't being run through or displayed. If there is a different strategy or approach to doing so, it'd be much appreciated!

2 Answers

Try the following regex pattern with "-replace" operator.

Since regex is greedy by default, it will try to get 32 characters + space first and then reduces the amount down to 0. (everything defined within the curly brackets: {0,32} )

To avoid a space after each line I used group constructs and only return the first group "$1"

$Runonsentence = 'Interestingly, Bryan A. Garners "The Oxford Dictionary of American Usage and Style" states that while there is a distinction between run-on sentences and comma splices, it isnt typically noteworthy. However, Garner also adds "The distinction can be helpful in differentiating between the wholly unacceptable (true run-on sentences) and the usually-but-not-always unacceptable (comma splices).'

$Runonsentence  -replace '(.{0,32})(?:\s+|$)', "`$1`r`n"

Don't forget the escape character in the replacement part. We don't want the PowerShell variable $1. So use `$1

You can use

$Runonsentence = $Runonsentence -replace '(.{32}\S*)\s+', "`$1`r`n"

See the regex demo.

Or, if you prefer to "go left" and insert line breaks after zero or up to 32 chars ending with a non-whitespace char, you may use

$Runonsentence = $Runonsentence -replace '(.{0,31}\S)\s+', "`$1`r`n"

See this demo.

  • (.{32}\S*) - Capturing group 1: any 32 chars other than a newline and then 0 or more chars other than whitespace
  • \s+ - one or more whitespace chars.

See the PS demo:

enter image description here
and
enter image description here

Related