且构网

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

Powershell中的Lambda表达式

更新时间:2023-11-11 11:56:22

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

或直接在方法调用中:

$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(...)