2

I'm extracting a four-part version number from a filename and converting it to a SemVer-compliant version.

  1. Filename: MyApp v2020.8.3.1.exe
  2. SemVer: 2020.8.3-r1

I've been successful at doing this using two steps:

sVersion = Regex.Match("MyApp v2020.8.3.1.exe", "[A-Za-z ]|\.[A-Za-z]").Value
oSemver = New Regex("\.(?=[^\.]+$)")
sSemVer = oSemver.Replace(sVersion, "-r")

This produces the desired SemVer-compliant version.

However, for the sake of brevity, I'd like to combine these two steps into a single operation.

I tried several variations on a couple of Q&As I found:

...but I didn't have much luck.

For example:

([A-Za-z ]|\.[A-Za-z])|(\.(?=[^\.]+$)) replaced with $2 produces only the four-part version number 2020.8.3.1.

Is this possible, given the constraints I'm working with?

--EDIT--

I only need a SemVer-compliant version number, not a new filename. In other words, I need to strip off all the extraneous characters (in this case MyApp v and .exe) and replace the final . in the four-part version number with -r.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
InteXX
  • 6,135
  • 6
  • 43
  • 80
  • What is the actual output here... Do you want a new filename, or just a version? are the input versions always 4 parts ? Maybe you can give a bunch of inputs and outputs. – TheGeneral Aug 04 '20 at 01:34
  • No, not a new filename. Just a version. See my edit. – InteXX Aug 04 '20 at 01:44

1 Answers1

1

If i understand what you want (and i am not sure i do)

Update

var input = "MyApp v2020.8.3.1.exe";
var m = Regex.Match(input, @"(\d+)\.(\d+)\.(\d+)\.(\d+)");
var result = $"{m.Groups[1]}.{m.Groups[2]}.{m.Groups[3]}-r{m.Groups[4]}";
Console.WriteLine(result);

Output

2020.8.3-r1

Full Demo Here

or you could just use (\d+\.\d+\.\d+)\.(\d+) which would result in 2 groups


Original

var input = "MyApp v2020.8.3.1.exe";
var result = Regex.Replace(input, @"(\d+)\.(\d+)\.(\d+)\.(\d+)", "$1.$2.$3-r$4");
Console.WriteLine(result);

Output

MyApp v2020.8.3-r1.exe

Full Demo Here

InteXX
  • 6,135
  • 6
  • 43
  • 80
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Close, but not quite. I need to extract `2020.8.3.1` from the filename and convert it to `2020.8.3-r1`. I only need the SemVer-compliant version, not a new filename. – InteXX Aug 04 '20 at 01:40
  • Beauteous! Now why didn't I think of that? – InteXX Aug 04 '20 at 01:46
  • 1
    I recently was able to push through the ceiling that was blocking me from figuring out the whole regular expressions paradigm. It's all starting to come clear to me now, and stuff like this really helps. Thanks much :-) – InteXX Aug 04 '20 at 01:54
  • @InteXX all good, I was hiding behind the door when they were handing out regular expression skills. – TheGeneral Aug 04 '20 at 01:55
  • Nifty neato :-) – InteXX Aug 04 '20 at 01:57
  • FYI I tuned up your 2-group expression a little bit. I like that one. That's the one I'm going to use. – InteXX Aug 04 '20 at 02:15