Reputation: 4109
I am trying to read a .sln
file and extract the strings that contain the path to the .csproj
within my solution.
The lines that contain the information that I am looking for look like this:
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project", "Project\Project.csproj", "{0DB516E6-4358-499D-BFBF-408F50A44E14}"
So, this is what I am trying:
$projectsInFile = Select-String "$slnFile" -pattern '^Project'
$csprojectsNames = $projectsInFile -replace ".+= `"(\S*) `""
Now, $csprojectsName
contain the information that I am looking for, but also the rest of the string.
Just like this:
Project\Project.csproj", "{0DB516E6-4358-499D-BFBF-408F50A44E14}"
What is the best way to retrieve the name of the .csproj
file without needing to manually cut the rest of the string?
Thank you
Upvotes: 1
Views: 156
Reputation: 46700
What you can do is capture the entire string and use a capture group in your replacement string thereby dropping the unneeded parts.
$csprojectsNames = $projectsInFile -replace '.+= "(\S*) "(.*?)",.*"','$2'
The second capture group is the data inbetween the quotes that follow = "Project", "....."
. Since it is the second capture group we replace the entire with that group '$2'
. Using single quotes ensure that PowerShell does not try to expand a variable.
Better approach
You might just be able to use [^"]*?\.csproj
in select-string
directly without having to do a secondary parse. That will match everything before .csproj that is not a quote so it wont gooble up too much.
Upvotes: 3
Reputation: 43743
You can use a group to capture the file path and then use the value of the group in as the replacement value. For instance:
$csprojectsNames = $projectsInFile -replace 'Project\(.*?\) = "Project", "(.*?)"', '$1'
Upvotes: 0