Regex 使用Powershell匹配文本文件中所有引用的模式
我有一个文本文件,我正在使用Powershell列出以下模式中的名称 文件内容:Regex 使用Powershell匹配文本文件中所有引用的模式,regex,powershell,match,Regex,Powershell,Match,我有一个文本文件,我正在使用Powershell列出以下模式中的名称 文件内容: beta-clickstream-class="owner:"mike"" beta-clickstream-class="owner:"kelly"" beta-clickstream-class="owner:"sam"" beta-clickstream-class="owner:"j
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
上面的脚本只列出了文件的名字。您能告诉我如何列出文件中的所有名称吗。
获取内容
返回一个字符串数组,因此您必须在数组的每个元素上调用[regex]::match()
但是,如所建议的,支持更简单的解决方案:
(Get-Content $importPath) -replace '.+owner:"([^&]+).+', '$1'
或者,您也可以将文件读入一个多行字符串,其中包含
Get Content-Raw
,后跟(多个匹配项),而不是(单个匹配项)。只需使用-replace
-->$string-replace$pattern,$1'