PowerShell Select-String来自带有Regex的文件

Sim*_*ice 4 regex string powershell substring

我试图从.sln(Visual Studio解决方案文件)中获取一串文本,以显示解决方案中包含的项目文件.

一个示例文本行是

项目("{xxxx-xxxxx-xxxx-xxxx-xx}")="partofname.Genesis.Printing","Production\partofname.Genesis.Printing\partofname.Genesis.Printing.csproj","{xxx-xxx-xxx -xxx-xxxx}"EndProject

我感兴趣的字符串部分是\和之间的最后一部分".

\ partofname.Genesis.Printing.csproj"

我正在使用的正则表达式是:

$r = [regex] "^[\\]{1}([A-Za-z.]*)[\""]{1}$"
Run Code Online (Sandbox Code Playgroud)

我正在阅读文件内容:

$sln = gci .\Product\solutionName.sln
Run Code Online (Sandbox Code Playgroud)

我不知道在我的string-select语句中放什么.

我是PowerShell的新手,非常感谢所有帮助......

我确实有一个非常长期的方式来做到这一点,但我已经失去了工作......基本上它是为文件中的每一行做这个:

Select-String $sln -pattern 'proj"' | ? {$_.split()}
Run Code Online (Sandbox Code Playgroud)

但正则表达式会更容易(我希望).

Swo*_*gan 14

下面获取之间的一切"proj":

$sln = Get-Content $PathToSolutionFile
$sln | Select-String ', "([^\\]*?\\)?([^\.]*\..*proj)"' -AllMatches | Foreach-Object {$_.Matches} | 
       Foreach-Object {$_.Groups[2].Value}
Run Code Online (Sandbox Code Playgroud)

第一组获取proj文件所在的文件夹.第二组获取您所请求的文件名(项目文件名).AllMatches返回每场比赛,而不仅仅是第一场比赛.之后,只需循环匹配对象​​上的每个匹配集合,并获得匹配中第二组的值.


tho*_*her 5

你的脚本很好用。要使其成为单行,请在 Select String 上添加 -Path:

Select-String -path $pathtoSolutionFile ', "([^\\]*?\\)?([^\.]*\..*proj)"' -
AllMatches | Foreach-Object {$_.Matches} | Foreach-Object {$_.Groups[2].Value}
Run Code Online (Sandbox Code Playgroud)

要从中构建,您可以使用 Groups[0]

(((Select-String -path $pathtoSoultionFile ', "([^\\]*?\\)?([^\.]*\..*proj)"' -AllMatches | Foreach-Object {$_.Matches} | 
       Foreach-Object {$_.Groups[0].Value})-replace ', "','.\').trim('"'))
Run Code Online (Sandbox Code Playgroud)