PowerShell Select-String from file with Regex [英] PowerShell Select-String from file with Regex

查看:49
本文介绍了PowerShell Select-String from file with Regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get a string of text from a .sln (Visual Studio solution file) to show what project files are contained within the solution.

文本行的示例是

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

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

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

The part of the string that I am interested in is the last part between the \ and the ".

\partofname.Genesis.Printing.csproj"

\partofname.Genesis.Printing.csproj"

我使用的正则表达式是:

The regular expression I am using is:

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

我正在阅读文件内容:

$sln = gci .\Product\solutionName.sln

我不知道在我的字符串选择语句中放什么.

I don't know what to put in my string-select statement.

我对 PowerShell 很陌生,希望得到任何帮助...

I am very new to PowerShell and would appreciate any and all help...

我之前确实有一个非常非常长的方法,但我已经失去了工作......本质上它是对文件中的每一行执行此操作:

I did have a very very long-hand way of doing this earlier, but I have lost the work... Essentially it was doing this for each line in a file:

Select-String $sln -pattern 'proj"' | ? {$_.split()}

但是正则表达式会容易得多(我希望).

But a regular expression would be a lot easier (I hope).

推荐答案

以下获取"proj"之间的所有内容:

The following gets everything between " and proj":

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

第一组获取项目文件所在的文件夹.第二组获取您所请求的(项目文件名).AllMatches 返回每个匹配项,而不仅仅是第一个.之后,只需遍历匹配对象上的每个匹配集合并获取匹配中第二组的值即可.

The first group gets the folder that the proj file is in. The second group gets just was you requested (the project file name). AllMatches returns every match, not just the first. After that it's just a matter of looping through each collection of matches on the match objects and getting the value of the second group in the match.

这篇关于PowerShell Select-String from file with Regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆