Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active June 27, 2018 12:23
Show Gist options
  • Save turboBasic/9b67d18142af081a91f876e3557f9d4c to your computer and use it in GitHub Desktop.
Save turboBasic/9b67d18142af081a91f876e3557f9d4c to your computer and use it in GitHub Desktop.
[Select-AllWordsMatch.ps1] Selects strings where all words are present in any order #powershell #textProcessing
function Select-AllWordsMatch {
[CmdletBinding()]
Param(
[Parameter(
Mandatory,
Position = 0,
ValueFromPipelineByPropertyName
)]
[Alias( 'Word' )]
[String[]] $Pattern,
[Parameter(
Mandatory,
Position = 1,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[Alias( 'Text' )]
[String[]] $InputObject,
[Switch] $WholeWords
)
Begin {
$delimiter = ''
if ($WholeWords) {
$delimiter = '\b'
}
$regexFilter = (
$Pattern |
ForEach-Object {
"(?=.*$delimiter$_$delimiter)"
}
) -join ''
"[Select-AllWordsMatch]: Perform matching with pattern $regexFilter" | Write-Verbose
}
Process {
foreach ($item in $InputObject) {
"[Select-AllWordsMatch]: Matching text $item" | Write-Verbose
if ($item -match $regexFilter) {
$item
}
}
}
End {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment