Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Select-String from file with Regex

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.

An example line of text is

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"

The regular expression I am using is:

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

I am reading the file content with:

$sln = gci .\Product\solutionName.sln

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

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).

like image 953
Simon Price Avatar asked Nov 12 '14 16:11

Simon Price


Video Answer


1 Answers

The following gets everything between " and proj":

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

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.

like image 85
Swoogan Avatar answered Oct 21 '22 09:10

Swoogan