Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用Powershell匹配文本文件中所有引用的模式_Regex_Powershell_Match - Fatal编程技术网

Regex 使用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

我有一个文本文件,我正在使用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

上面的脚本只列出了文件的名字。您能告诉我如何列出文件中的所有名称吗。

获取内容
返回一个字符串数组,因此您必须在数组的每个元素上调用
[regex]::match()

但是,如所建议的,支持更简单的解决方案:

(Get-Content $importPath) -replace '.+owner:"([^&]+).+', '$1'

或者,您也可以将文件读入一个多行字符串,其中包含
Get Content-Raw
,后跟(多个匹配项),而不是(单个匹配项)。

只需使用
-replace
-->
$string-replace$pattern,$1'