Overview

$linesWithPattern = $lines | where {$_ -match "block"}
$occurrences = [RegEx]::Matches($multiline, $PATTERN).value
$data = [RegEx]::Matches($multiline, $PATTERN) | foreach { 
    [PSCustomObject]@{
        FirstCaptureGroup = $_.Groups[1].value
        NamedCaptureGroup = $_.Groups[$groupName].value
    }
}

Filter lines than contain a pattern

single line mode

$linesWithBlock = $lines | where {$_ -like "*block*"}
$linesWithBlock = $lines | where {$_ -match "block"}
$linesWithStandaloneWordBlock = $text | where {
    $_ -match '(?<!\w)' + 'block' + '(?!\w)'
}
  • see Examples

Find occurrences of a pattern

single line mode

$occurrences = $lines | foreach {
    [RegEx]::Match($_, $PATTERN) 
} | where Success | foreach {
    $_.Value
}

multi line mode

$occurrences = [RegEx]::Matches($multiline, $PATTERN).value
  • see Examples

Capture multiple optionally named data points

single line mode

$data = $lines | foreach {
    [RegEx]::Match($_, $PATTERN) 
} | where Success | foreach { 
    [PSCustomObject]@{
        FirstCaptureGroup = $_.Groups[1].value
        NamedCaptureGroup = $_.Groups[$groupName].value
    }
}

multi line mode

$data = [RegEx]::Matches($multiline, $PATTERN) | foreach { 
    [PSCustomObject]@{
        FirstCaptureGroup = $_.Groups[1].value
        NamedCaptureGroup = $_.Groups[$groupName].value
    }
}

Performance

Examples

Regular Expressions - Search, extract and manipulate text in a specified pattern > Quick Reference

Read example file

$helpFile = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\en-US\about_Calculated_Properties.help.txt"
$lines = Get-Content -Path $helpFile
$multiline = (Get-Content -Path $helpFile) -join "`n"
$multiline = (Get-Content -Path $helpFile -Raw) -replace [Environment]::NewLine, "`n"

Example: Get paragraph after a standalone Short or long description “heading”

# line example          Short·description`n`nCAPTURE
$PATTERN = '(?m)' + '(?<=^\w+ description\n\n)' + '(.+\n)+'
[RegEx]::Matches($multiline, $PATTERN).value

Example: Get table column with name, definition, and parameter count of aliases

# line example        ···clear···Clear-Host··0··
$PATTERN = '(?m)' + '^\s*(\w+)\s+' + '(?<Definition>\w+-\w+)' + '\s+(?<ParameterCount>\d+)' + '\s*$'
[RegEx]::Matches($multiline, $PATTERN) | foreach { 
    [PSCustomObject]@{
        Name = $_.Groups[1].value
        Definition = $_.Groups["Definition"].value
        ParameterCount = $_.Groups["ParameterCount"].value
    }
}

Sources:

Related:
Regular Expressions - Search, extract and manipulate text in a specified pattern

Tags:
PowerShell Objects - Handle, Import, Export, Filter and RegEx query objects
Document conversion