1

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!

1
  • -split (?m)^\r?\n could work Commented Mar 10, 2022 at 21:02

1 Answer 1

3

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()
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.