Anchors in mutli-line regular expressions with PowerShell -match

Will Pittenger 281 Reputation points
2022-10-04T03:47:01.093+00:00

With -match, how do you tell it you want a Multi-Line match? https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about\_regular\_expressions?view=powershell-7.2#anchors doesn't describe that. Instead, it just links to a general .NET discussion that assumes you're working directly with those types.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2022-10-04T15:35:10.48+00:00

    You can use the "inline" regex method:

    $mult = "aaa`nbbb`nccc`naaa`n"  
      
    $mult -match "(?m)^(aaa)$"  # TRUE -- two results in $matches  
    $Matches  
    "------------------"  
    $mult -match "(?s)^(aaa)$"  #FALSE -- two results in $matches  
    $matches  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful