Select-String -Pattern '(?:website data )(.*)(?:more data )([regex]::match($selected))(?: data)' -AllMatches

问题描述 投票:0回答:1
$selected = 'File Name 123'
(Invoke-WebRequest -URI "$data").Content | 
Select-String -Pattern '(?:website data )(.*)(?:more data )([regex]::match($selected))(?: data)' -AllMatches | 
ForEach-Object { $_.Matches } | ForEach-Object { $_.Groups[2].Value }

如果我更换 ([regex]::match($selected))(.*) 然后我会得到正确的结果,但我需要将$selected变量的文本插入到该组中,以找到正确的Groups[1]的值,这可能吗,因为我的代码现在不工作?

EDIT

多亏了Olaf,我修好了脚本,现在可以用了。

$regex = '(?:website data )(.*)(?:more data )' + "($selected)" + '(?: data)'
(Invoke-WebRequest -URI "$data").Content |
Select-String -Pattern $regex -AllMatches | 
ForEach-Object { $_.Matches } | ForEach-Object { $_.Groups[2].Value }

powershell
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.