Home > Back-end >  Powershell - Create array from string - separating blocks of text by new lines
Powershell - Create array from string - separating blocks of text by new lines

Time:03-11

I have a string in a variable:

$Test = @"
Paragraph 1: some paragraph text
some paragraph text some paragraph text
some paragraph text some paragraph text

Paragraph 2: some paragraph text
some paragraph text some paragraph text
some paragraph text some paragraph text

Paragraph 3: some paragraph text
some paragraph text some paragraph text
some paragraph text some paragraph text
"@

There might be an undetermined number of paragraphs.

The paragraphs are always separated by a new line.

I need each paragraph block of text separated into a new array variable.

Example:

$Array[1] should give me the entire block of text of Paragraph 2.

Thank you!

CodePudding user response:

Split by two or more consecutive newlines:

# Pipe to 
#  | ForEach-Object { "[$_]" } 
# to visualize the resulting array elements.
$test -split '(?:\r?\n){2,}'

For an explanation of the regex passed to the -split operator and the ability to experiment with it, see this regex101.com page.

Note:

  • If you can assume that only Unix-format LF (\n) newlines are present in your input string, you can simplify the regex to '\n{2,}' - the regex above handles both LF and Windows CRLF (\r\n) newlines.

  • If the last paragraph happens to contain a single trailing newline (as would typically happen if you read a text file in full with Get-Content -Raw) that you want to eliminate from the array, use:

    # Note: If you need to preserve trailing *non-newline* whitespace,
    #       use .TrimEnd("`r", "`n") instead.
    ($test -split '(?:\r?\n){2,}').TrimEnd()
    
  • Related