1

I'm trying to use a powershell regex to pull version data from the AssemblyInfo.cs file. The regex below is my best attempt, however it only pulls the string [assembly: AssemblyVersion(". I've put this regex into a couple web regex testers and it LOOKS like it's doing what I want, however this is my first crack at using a powershell regex so I could be looking at it wrong.

$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '([^"]+)"').Groups[1].Value
NealR
  • 10,189
  • 61
  • 159
  • 299

4 Answers4

6

You also need to include the starting double quotes otherwise it would start capturing from the start until the first " is reached.

$prog = [regex]::match($s, '"([^"]+)"').Groups[1].Value
                            ^
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
6

Try this regex "([^"]+)"

Regex101 Demo

  • You can also use `(\d+\.?)+` [Demo here](https://www.regex101.com/r/qI4wG6/3) if you just want the version no. –  Mar 24 '16 at 12:18
2

Regular expressions can get hard to read, so best practice is to make them as simple as they can be while still solving all possible cases you might see. You are trying to retrieve the only numerical sequence in the entire string, so we should look for that and bypass using groups.

$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '[\d\.]+').Value

$prog

1.0.0.0
doron
  • 258
  • 2
  • 11
0

For the generic solution of data between double quotes, the other answers are great. If I were parsing AssemblyInfo.cs for the version string however, I would be more explicit.

$versionString = [regex]::match($s, 'AssemblyVersion.*([0-9].[0-9].[0-9].[0-9])').Groups[1].Value
$version = [version]$versionString

$versionString

1.0.0.0

$version

Major  Minor  Build  Revision
-----  -----  -----  --------
1      0      0      0    

Update/Edit:

Related to parsing the version (again, if this is not a generic question about parsing text between double quotes) is that I would not actually have a version in the format of M.m.b.r in my file because I have always found that Major.minor are enough, and by using a format like 1.2.* gives you some extra information without any effort.

See Compile date and time and Can I generate the compile date in my C# code to determine the expiry for a demo version?.

When using a * for the third and fourth part of the assembly version, then these two parts are set automatically at compile time to the following values:

third part is the number of days since 2000-01-01

fourth part is the number of seconds since midnight divided by two (although some MSDN pages say it is a random number)

Something to think about I guess in the larger picture of versions, requiring 1.2.*, allowing 1.2, or 1.2.3, or only accepting 1.2.3.4, etc.

Community
  • 1
  • 1
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • I would suggest replacing `[0-9]` with the `/d` wildcard for digits. You also need to use a `+` in the event that any of the components (major, minor, build, revision) go to two digits like this: **5.12.1.11**. – doron Mar 24 '16 at 15:19
  • @doron, good feedback, thank you. I have updated my answer to reflect what I do in practice instead of trying to parse every combination of number, which now makes me think of scanning other/older projects for "undesired" version strings to use my preferred methods. Thanks again for the thought-provoking comment. – Kory Gill Mar 24 '16 at 17:16