在 C# 中的另一个字符串中查找多个值和字符串

所以我有这个有 4 行的字符串:


id  score ping guid       name            lastmsg address               qport  rate

--- ----- ---- ---------- --------------- ------- --------------------- ------ -----

  1     11  45 176387877  Player 1        3250    101.102.103.104:555   3647   25000

  2     23  61 425716719  Player 2        3250    105.106.107.108:555   5978   25000

我怎样才能“提取”所有这些值?比如,我想保存“id”、“score”、“ping”、“guid”、“name”等。


我玩过在这里找到的“GetBetween”功能。我也尝试学习 string.Split 函数。但我认为我没有接近我想要存档的内容,而且我还不太了解拆分字符串。


我基本上需要删除值之间的所有“”空格,问题是值长度可能会改变,例如“名称”。


有人可以给我一个如何提取值的例子吗?


炎炎设计
浏览 211回答 2
2回答

慕标5832272

RegEx.Split是你的朋友,这很好用。void Main(){    // fun fact, the @ in front of the string means it's literal, so you    // literally get the new lines    var input =     @"id  score ping guid       name            lastmsg address               qport  rate-- - -------------------------------------------------------------------------  1     11  45 176387877  Player 1        3250    101.102.103.104:555   3647   25000  2     23  61 425716719  Player 2        3250    105.106.107.108:555   5978   25000";    //Gets you each line    var lines = input.Split('\n');    // Skip 2 because I doubt you care about the column title     // or the row with the dashes    foreach (var line in lines.Skip(2))     {      // For each line, Regex split will return an array with each entry      // Set a breakpoint with the debugger and inspect to see what I mean.      // Splits using regex - assumes at least 2 spaces between items       // so space in 'Player 1' is handled it's a fickle solution though      // Trim the line before RegEx split to avoid extra data in the split       var r = Regex.Split(line.Trim(), @"\s{2,}");     }}

MM们

您可以使用 Regex 和命名组来执行此操作。样本输入&nbsp; &nbsp; var str = @"id&nbsp; score ping guid&nbsp; &nbsp; &nbsp; &nbsp;name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastmsg address&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qport&nbsp; rate--- ----- ---- ---------- --------------- ------- --------------------- ------ -----&nbsp; 1&nbsp; &nbsp; &nbsp;11&nbsp; 45 176387877&nbsp; Player 1&nbsp; &nbsp; &nbsp; &nbsp; 3250&nbsp; &nbsp; 101.102.103.104:555&nbsp; &nbsp;3647&nbsp; &nbsp;25000&nbsp; 2&nbsp; &nbsp; &nbsp;23&nbsp; 61 425716719&nbsp; Player 2&nbsp; &nbsp; &nbsp; &nbsp; 3250&nbsp; &nbsp; 105.106.107.108:555&nbsp; &nbsp;5978&nbsp; &nbsp;25000";正则表达式定义var regex = new Regex(@"^(?<id>[\d]+)(\s{2,})(?<score>[\d]+)(\s{2,})(?<ping>[\d]+)(\s{1,})(?<guid>[\d]+)(\s{2,})(?<name>([\w]+\s[\w]+))(\s{2,})(?<lastmsg>[\d]+)(\s{2,})(?<ip>[\d.:]+)(\s{2,})(?<port>[\d]+)(\s{2,})(?<rate>[\d]+)$",RegexOptions.Compiled);解析代码var lines = str.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);foreach(var line in lines){&nbsp; &nbsp; var match = regex.Match(line.Trim());&nbsp; &nbsp; if(!match.Success) continue;&nbsp; &nbsp; Console.WriteLine($"ID = {match.Groups["id"].Value}");&nbsp; &nbsp; Console.WriteLine($"Score = {match.Groups["score"].Value}");&nbsp; &nbsp; Console.WriteLine($"Ping = {match.Groups["ping"].Value}");&nbsp; &nbsp; Console.WriteLine($"Guid = {match.Groups["guid"].Value}");&nbsp; &nbsp; Console.WriteLine($"Name = {match.Groups["name"].Value}");&nbsp; &nbsp; Console.WriteLine($"Last Msg = {match.Groups["lastmsg"].Value}");&nbsp; &nbsp; Console.WriteLine($"Port = {match.Groups["port"].Value}");&nbsp; &nbsp; Console.WriteLine($"Rate = {match.Groups["rate"].Value}");}输出ID = 1Score = 11Ping = 45Guid = 176387877Name = Player 1Last Msg = 3250Port = 3647Rate = 25000ID = 2Score = 23Ping = 61Guid = 425716719Name = Player 2Last Msg = 3250Port = 5978Rate = 25000
打开App,查看更多内容
随时随地看视频慕课网APP