从字符串中获取一些数据[] [英] Get some data from string[]

查看:74
本文介绍了从字符串中获取一些数据[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我正在开发MOHAA游戏的管理工具.我需要一些帮助,以从status命令的返回中获取数据.当您发送状态命令时,服务器将返回以下内容:

Hi guys! I''m developing an admin tool for MOHAA game. I need some help to get data from the return of status command. when you send status command the server return something like this:

map: dm/mohdm6
num score ping name            lastmsg address               qport rate
--- ----- ---- --------------- ------- --------------------- ----- -----
  0    52   51 WILDGERT              0 80.57.73.77:12203     51668 30000
  1    79   58 AMF # pAwLaS          0 213.17.188.129:12203   2006 20000
  2    52  348 Captain_Frank's_AR      0 190.49.36.201:11210   56094 25000
  3    79   63 Mads                 50 90.185.34.32:12203    23266 30000
  4    52   50 AMF # Krystian        0 212.67.137.147:12203  35791 25000
  5    52   48 nuckleaf              0 90.176.101.17:11803   10622 30000
  6    79   52 Cpt.atikaftc         50 88.132.162.114:12203  41913 30000
  7    79   98 D t S.                0 62.182.225.61:-20181   4081 25000
  8    52  460 James_Bond_007        0 64.116.185.9:25505    62462  4000
  9    52  266 almoftary            50 82.201.229.226:12203  54527 30000
 10    79  148 Hadiation             0 41.239.54.42:12203     2657 90000




我想获取播放器编号(第一个数字),播放器名称(例如WOLDGERT)和播放器ip(例如80.57.73.77).我的问题是,当玩家的名字中有空格时,我不能在每一行中使用空格来分割成num score ping ecc ...什么是获取我所需数据的最佳方法而又不会因为名字中的空格而出现问题?? Ty




I want to get player number (first number), player name (eg WOLDGERT) and player ip(eg. 80.57.73.77). My problem is that when a player has spaces in his name i cannot use spaces in each line to split into num score ping ecc... What''s the best way to get the data i need without having problems due to spaces in name?? Ty

推荐答案

Regex是您的朋友;-)

解析整行并选择您需要的内容,例如:

Regex is your friend ;-)

Parse the whole line and pick what you need, e.g.:

string file = ...
Regex rex = new Regex(
      @"^\s*(\d+)\s+\S+\s+\S+\s+(.+?)\s+\S+\s+(\S+?):\S+\s+\S+\s+\S+\s*


", RegexOptions.Compiled); var q = 来自文件中的行. ReadAllLines(文件) match = rex.Match(行) 其中匹配.成功 选择 Tuple< int,字符串,字符串>( int .Parse(match.Groups [ 1 ].Value), match.Groups [ 2 ].Value, match.Groups [ 3 ].Value); foreach ( var 条目 in 中的q) { Console.WriteLine(" , entry.Item1,entry.Item2,entry.Item3); }
", RegexOptions.Compiled); var q = from line in File.ReadAllLines(file) let match = rex.Match(line) where match.Success select new Tuple<int, string, string>( int.Parse(match.Groups[1].Value), match.Groups[2].Value, match.Groups[3].Value); foreach (var entry in q) { Console.WriteLine("{0,3}: {1,-20} @{2}", entry.Item1, entry.Item2, entry.Item3); }



示例代码的输出为:



The output for your sample code is:

 0: WILDGERT             @80.57.73.77
 1: AMF # pAwLaS         @213.17.188.129
 2: Captain_Frank's_AR   @190.49.36.201
 3: Mads                 @90.185.34.32
 4: AMF # Krystian       @212.67.137.147
 5: nuckleaf             @90.176.101.17
 6: Cpt.atikaftc         @88.132.162.114
 7: D t S.               @62.182.225.61
 8: James_Bond_007       @64.116.185.9
 9: almoftary            @82.201.229.226
10: Hadiation            @41.239.54.42


最简单吗?
如果格式始终相同,则可以使用拆分"将其分成几行,然后跳过简介(只需跳过,直到获得以-"开头的行)即可.
然后,您可以使用Substring提取相应的字段.
Easiest?
If the format is always the same, you can use Split to break it to individual lines, then skip the intro (just skip until you get a line starting with ''-'')
Then you can just use Substring to extract the appropriate fields.


这篇关于从字符串中获取一些数据[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆