Powershell Regex匹配为字符串

时间:2016-07-07 15:56:50

标签: regex powershell

给出一个字符串:

$assemblyVersion = '[assembly: AssemblyFileVersion("8.0.0.866")]'

我有一个可以找到程序集号的正则表达式:

(\d+\.)(\d+\.)(\d+\.)(\*|\d+)

这些行确认已找到匹配项:

$extractedVersion = $assemblyVersion -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";

然后如何从原始字符串$assemblyVersion中提取匹配项?

阅读this article我不知道你是怎么做的......

我试过了:

$assemblyVersion

$extractedVersion = $assemblyVersion -match "?<assembly>(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";

Write-Host "Extracted " $extractedVersion["assembly"]

不起作用。

我的正则表达式可能也不是最优的。

此stackoverflow answer无效

1 个答案:

答案 0 :(得分:0)

我明白了:

$assemblyVersion = '[assembly: AssemblyFileVersion("8.0.0.866")]'

$assemblyVersion -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";

Write-Host "Extracted " $matches[0]