0

Problem

I am writing a script that automates the Version Control for a series of projects. What I am trying to achieve is to store the current value of the AssemblyVersion found within the AssemblyInfo.cs file into a variable.

E.G.: The line is [assembly: AssemblyVersion("1.0.0.0")] and I want to extract/store whatever value is between AssemblyVersion(" and ")]

I understand that regular expressions will most likely be needed here, but I am having trouble understanding how to target values between two specific values. I attempted to read through the Regular-Expression.info website in hopes of understanding this better. In this, I read through Lookahead and Lookbehind as one such possibility, but didn't understand how to combine these expressions and there were little to no examples to get a strong grasp on how to these are used. I also thumbed through matching Complete Lines, but this also seemed to put me in a position where I would have use two separate equations to try and get the results I desire...

In my research, I found one similar approach at: Match everything between two words in PowerShell. Unfortunately, despite the mini explanation provided, I could not modify and duplicate the results to suite my need. I often encountered an error stating: 'cannot convert to regex'. I was not able to figure out what I was doing wrong and it didn't help that there was no explanation as to what the $subject variable was referring to.

Continuing my research, the other similar variants that others asked of the above have very case specific answers that I have not been able to utilize.

I am hoping that someone here could help me to answer my question with an answer in which each piece is understandable enough to modify the answer to fit future similar problems.

Question

How can I get the value, despite the value type, that is between two strings so that I can reliably grab the AssemblyVersion value with a single equation that is scalable enough to not care about the length of characters found? Could you also be kind enough to explain the pieces of the answer so that I can understand how you came to it?

Brandon
  • 731
  • 2
  • 12
  • 29
  • 2
    [`(?<=AssemblyVersion\(")[^"]*`](https://regex101.com/r/htXhK5/1) should work. – ctwheels Nov 13 '17 at 16:52
  • *"I do not fully understand regex enough to generate this myself"* - You don't have to understand regex *fully* to do this. And as an excuse to not even *try* it's a little thin. There are so many great regex resources (https://www.regular-expressions.info/, https://regex101.com to name only two) that you should be getting somewhere in an hour of work. It can't be that you give up after not finding anything you can copy and paste and then come here to wait for someone to write something you can copy and paste, if you know what I mean. – Tomalak Nov 13 '17 at 16:53
  • 1
    @Tomalak After re-reading my above problem/question, I realize how it sounds and should have worded that better. I am not seeking a Copy/Paste answer, I would appreciate an answer in which the pieces are explained, not simply given. I prefer to understand the answer so that I can duplicate, modify, and add complexity to it for future problems/situations. BEFORE I came here seeking guidance, I spent 5 hours working on this problem. Now, as a 'respectable' member of the community, how about you not bash someone's question, and instead attempt to help guide and answer it instead? – Brandon Nov 13 '17 at 17:10
  • @ctwheels I appreciate the submission, but I still don't understand what the pieces of that code does. Would you mind explaining the pieces of it so that I can understand how it works? – Brandon Nov 13 '17 at 17:12
  • @Brandon if you open the link it does a good job of explaining it. Basically, though, it's a positive lookbehind ensuring what precedes matches `AssemblyVersion("` (note that `(` needs to be escaped as it has special meaning in regex. `[^"]*` Matches any number of any character not in the list (so any character except `"`) – ctwheels Nov 13 '17 at 17:20
  • @ctwheels OH! How did I not notice that the response was a link? Hmm... I see that makes sense. I'm gonna have to play with this. Thanks – Brandon Nov 13 '17 at 17:31
  • If you have spent 5 hours on this, you certainly have something to show for it, right? You can't have not a single lead after 5 hours of work, that's impossible. I appreciate that you did your part, the question is just missing it. That being said, I am not "bashing" your question, I am vocalizing actionable criticism, because I think it's not lost on you. That's a huge difference. "Show where you are stuck" is basically on page 1 of the FAQ. https://stackoverflow.com/help/how-to-ask – Tomalak Nov 13 '17 at 18:16
  • @Tomalak Criticism can still come off as bashing, despite the reasons you are doing it. People come here looking for help, not a verbal Gibb Slap. If it would make you feel better: I had begun with those resources already, but failed for over 3 hours to get the results in PS that I was trying to achieve. Thinking I was missing something, I went to the net for research. After 2 hours, I had 6-7 promising leads, all of which turned out to be dead ends too for one reason or another. Feeling I had exhausted my research, I came here to ask my question. I am sorry I did not fill my post with proof. – Brandon Nov 13 '17 at 20:10

1 Answers1

2

Not always necessary to use regex. In your case, you can use split with double-quote like delimiter, like it:

$str='[assembly: AssemblyVersion = ''[assembly: AssemblyVersion("1.0.0.0")] AssemblyVersion("")]'
($str -split '"')[1]
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • `$version = ($str -split '"') -like '[0-9].[0-9].[0-9].[0-9]'` would be the non-regex, position-agnostic variant. – Tomalak Nov 14 '17 at 11:06
  • Thanks, I had never considered this as a possibility. It works out in a surprisingly cleaner way than I was trying to do this. – Brandon Dec 06 '17 at 14:03