我有一个文本文件,我正在使用Powershell列出下面模式中的名称
档案内容:
beta-clickstream-class="owner:"mike""
beta-clickstream-class="owner:"kelly""
beta-clickstream-class="owner:"sam""
beta-clickstream-class="owner:"joe""
beta-clickstream-class="owner:"john""
beta-clickstream-class="owner:"tam""我想要的输出
mike
kelly
sam
joe
john
tam我正在使用的脚本
$importPath = "test.txt"
$pattern = 'beta-clickstream-class="owner:"(.*?)""'
$string = Get-Content $importPath
$result = [regex]::match($string, $pattern).Groups[1].Value
$result上面的脚本只列出文件上的第一个名称。你能指导我如何列出文件上的所有名字吗?
发布于 2020-04-10 16:30:27
Get-Content返回一个字符串数组,因此您必须对数组$string的每个元素调用[regex]::match()。
但是,正如-replace operator所建议的那样,AdminOfThings提供了一个更简单的解决方案:
(Get-Content $importPath) -replace '.+owner:"([^&]+).+', '$1'或者,您可以使用Get-Content -Raw将文件读入一个单一的多行字符串中,然后是[regex]::Matches() (多个匹配),而不是[regex]::Match() (单个匹配)。
https://stackoverflow.com/questions/61144374
复制相似问题