Problem
I am working on an automated version control script in powershell and I hit a snag where I am trying pull a line of text from an AssemblyInfo.cs file. However, I can't seem to get it to work as expected despite all my efforts.
After many trial and errors to achieve the results intended, I have come to find the following to get close to what I'm trying to achieve:
# Constants
$Assembly = Get-Item "C:\generalfilepath\*\Assembly.cs"
$regex = '(?<!\/\/ \[assembly: AssemblyVersion\(")(?<=\[assembly: AssemblyVersion\(")[^"]*'
$CurrentVersion = GC $Assembly
$CurrentVersion = $CurrentVersion -match $regex
Write-Host "CurrentVersion = $CurrentVersion
I am expecting to see: CurrentVersion = 1.0.0.0 or something similar, but what I get is: CurrentVersion = [assembly: AssemblyVersion("1.0.0.0")]
I have been searching all over for examples on how to properly utilize regex with PowerShell, but I have not really found anything that contains more than the regex itself... As such, I was hoping someone here could assist me in either steering me in the right direction or point out what I am doing wrong.
Question
Despite the use of the regex filter, I'm getting the entire line instead of just the value I want. How do I ensure the variable stores ONLY the target value using the lookbehind and lookahead regex filters?
Get-Contentreturns an array of lines,-matchon an array of lines filters it for the lines which match the regex. It's not clear if you're expecting one file, since you use a wildcard in the path, but however you do it, you need to reference$Matchesafter using-matchto get the match details. ProbablyGet-Content -Rawto get a single string, and-matchand then$matches[1]