Lambda Expression in Powershell

In PowerShell 2.0 you can use a script block ({ some code here }) as delegate:

$MatchEvaluator = 
{  
  param($m) 

  if ($m.Groups["val"].Value -eq ";") 
  { 
    #... 
  }
}

$result = $r.Replace($input, $MatchEvaluator)

Or directly in the method call:

$result = $r.Replace($input, { param ($m) bla })

Tip:

You can use [regex] to convert a string to a regular expression:

$r = [regex]"\((?<val>[\,\!\;\:])\)"
$r.Matches(...)

Leave a Comment