Last active
          June 27, 2018 12:23 
        
      - 
      
- 
        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
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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