且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 中的 Lambda 表达式

更新时间:2023-11-11 12:26:52

在 PowerShell 2.0 中,您可以使用脚本块 ({ some code here }) 作为委托:

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 })

提示:

您可以使用[regex]将字符串转换为正则表达式:

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

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