Search code examples
c#regexparsingcomparisonversion

How to extract name and version from string


I have many filenames such as:

libgcc1-5.2.0-r0.70413e92.rbt.xar
python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar
u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar

I need to reliably extract the name, version and "rbt" or "norbt" from this. What is the best way? I am trying regex, something like:

(?<fileName>.*?)-(?<version>.+).(rbt|norbt).xar

Issue is the file name and version both can have multiple semi colons. So I am not sure if there is an answer by I have two questions:

  1. What is the best strategy to extract values such as these?
  2. How would I be able to figure out which version is greater?

Expected output is:

libgcc1, 5.2.0-r0.70413e92, rbt
python3-sqlite3, 3.4.3-r1.0.f25d9e76, rbt
u-boot-signed-pad.bin, v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57, rbt

Solution

  • This will give you what you want without using Regex:

    var fileNames = new List<string>(){
        "libgcc1-5.2.0-r0.70413e92.rbt.xar",
        "python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",
        "u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"
    };
    foreach(var file in fileNames){
        var spl = file.Split('-');
        string name = string.Join("-",spl.Take(spl.Length-2));
        string versionRbt = string.Join("-",spl.Skip(spl.Length-2));
        string rbtNorbt = versionRbt.IndexOf("norbt") > 0 ? "norbt" : "rbt";
        string version = versionRbt.Replace($".{rbtNorbt}.xar","");
        Console.WriteLine($"name={name};version={version};rbt={rbtNorbt}");
    }
    

    Output:

    name=libgcc1;version=5.2.0-r0.70413e92;rbt=rbt
    name=python3-sqlite3;version=3.4.3-r1.0.f25d9e76;rbt=rbt
    name=u-boot-signed-pad.bin;version=v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57;rbt=rbt
    

    Edit:

    Or using Regex:

    var m = Regex.Match(file,@"^(?<fileName>.*)-(?<version>.+-.+)\.(rbt|norbt)\.xar$");
    string name = m.Groups["fileName"].Value;
    string version = m.Groups["version"].Value;
    string rbtNorbt = m.Groups[1].Value;
    

    The output will be the same. Both approaches assum that "version" has one -.