猿问

如何从字符串中提取名称和版本

我有很多文件名,例如:


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

我需要从中可靠地提取名称、版本和“rbt”或“norbt”。什么是最好的方法?我正在尝试正则表达式,例如:


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

问题是文件名和版本都可以有多个分号。所以我不确定是否有答案我有两个问题:


提取这些价值的最佳策略是什么?

我怎样才能找出哪个版本更大?

预期输出是:


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


慕慕森
浏览 99回答 2
2回答

开满天机

这将在不使用 Regex 的情况下为您提供所需的内容:var fileNames = new List<string>(){&nbsp; &nbsp; "libgcc1-5.2.0-r0.70413e92.rbt.xar",&nbsp; &nbsp; "python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",&nbsp; &nbsp; "u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"};foreach(var file in fileNames){&nbsp; &nbsp; var spl = file.Split('-');&nbsp; &nbsp; string name = string.Join("-",spl.Take(spl.Length-2));&nbsp; &nbsp; string versionRbt = string.Join("-",spl.Skip(spl.Length-2));&nbsp; &nbsp; string rbtNorbt = versionRbt.IndexOf("norbt") > 0 ? "norbt" : "rbt";&nbsp; &nbsp; string version = versionRbt.Replace($".{rbtNorbt}.xar","");&nbsp; &nbsp; Console.WriteLine($"name={name};version={version};rbt={rbtNorbt}");}输出:name=libgcc1;version=5.2.0-r0.70413e92;rbt=rbtname=python3-sqlite3;version=3.4.3-r1.0.f25d9e76;rbt=rbtname=u-boot-signed-pad.bin;version=v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57;rbt=rbt编辑:或者使用正则表达式: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;输出将是相同的。这两种方法都假设“版本”有一个-.

MYYA

测试了以下代码并与 Regex 完美配合。我使用了从右到左的选项using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication107{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] inputs = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "libgcc1-5.2.0-r0.70413e92.rbt.xar",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string pattern = @"(?'prefix'.+)-(?'middle'[^-][\w+\.]+-[\w+\.]+)\.(?'extension'[^\.]+).\.xar";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string input in inputs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("prefix : '{0}', middle : '{1}', extension : '{2}'",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match.Groups["prefix"].Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match.Groups["middle"].Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match.Groups["extension"].Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答