如何从文本文件中提取子字符串?

Al *_*aba 4 powershell

我有以下文本文件

testA=foo
testB=foobar
testC=Whatever
Run Code Online (Sandbox Code Playgroud)

现在我想提取的值testBfoobar.使用我当前的PowerShell命令,我只能收到整行:

testB=foobar
Run Code Online (Sandbox Code Playgroud)

我的命令是:

Select-String -Path .\test.txt -Pattern "testB=" -List
Run Code Online (Sandbox Code Playgroud)

我可以看到字符串有替换函数,所以你可以testB=用空字符串替换,但我不知道如何实现这一点.

Mar*_*ndl 5

不必替换任何内容来捕获值.而是将捕获组添加到搜索模式中.然后您就可以访问捕获的值:

(Select-String -Path $scripts.tmp -Pattern "testB=(.*)").Matches.Groups[1].Value
Run Code Online (Sandbox Code Playgroud)