PowerShell regex matches not being picked up

Rodrigo Dumont's Avatar

Rodrigo Dumont

30 Jun, 2014 06:58 PM

The following is part of a PowerShell script that I have running as After build script. The two echos in the end are just for debugging this problem.

$file = get-content src\ProductAssemblyInfo.cs
$file -match "AssemblyInformationalVersion\(""(.+?)""\)]"
$semver = $matches[1]
echo $file
echo $matches

I expected $matches to contain the matches for the -match operation in the previous line, but it doesn't. Here is what is actually printed:

using System.Reflection;
[assembly: AssemblyVersion("0.1.0")]
[assembly: AssemblyFileVersion("0.1.0")]
[assembly: AssemblyInformationalVersion("0.1.0-alpha")]
Key   : 2
Value : C:\Program Files (x86)\Common Files\Microsoft Shared\XNA\
Name  : 2

Key   : 1
Value : XNAGSShared
Name  : 1

Key   : 0
Value : XNAGSShared=C:\Program Files (x86)\Common Files\Microsoft Shared\XNA\
Name  : 0

So, you can see the contents of $file, as expected, and $matches, which has some value that means nothing to me. Something is bizarrely wrong here, right? :p

  1. Support Staff 1 Posted by Feodor Fitsner on 01 Jul, 2014 04:38 AM

    Feodor Fitsner's Avatar

    This construction can work better:

    [Text.RegularExpressions.Regex]::Match($file, "AssemblyVersion\(""(.+?)""\)]")
  2. 2 Posted by Rodrigo Dumont on 01 Jul, 2014 06:05 PM

    Rodrigo Dumont's Avatar

    Sure, I just used the [regex] syntax, which is much nicer anyway. I'm not too familiar with PowerShell, so I just thought the described behavior was very weird.

    It isn't actually getting in my way right now, but I think it is something that should be investigated, since more people might end up running into the same situation.

  3. Support Staff 3 Posted by Feodor Fitsner on 01 Jul, 2014 06:17 PM

    Feodor Fitsner's Avatar

    Yeah, we use more sophisticated regex for replacing version information. Can share it if you need it.

  4. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 01:45 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac