我是 powershell 写脚本的新手,尤其对正则表达式了解甚少,请帮忙实现一下。
例子:
PS > .\test.ps1 "A Balrog is a powerful fictional monster in Middle-earth."
return: Middle-earth
或者
PS >.\test.ps1 "The symbol underscore, also called low_line or low dash."
return: low_line
当然,我尝试找到解决这个问题的方法,以下是我的解决方案:
#$data = $args[0]
$p = '\w*[-_]\w*'
Select-String -Path .\content.txt -Pattern $p | % {$_ -match $p; $matches.Value}
Write-Output $matches
============================================== 更新:找到解决方案
答案1
$data = $args[0]
$pattern = '\w*[-_]\w*'
$matches = [regex]::Matches($data, $pattern).Value
Write-Output $matches