-1

So I have this string with 4 lines:

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

How can I 'extract' all of these values? Like, I want to save "id", "score", "ping", "guid", "name", etc.

I have played around with a "GetBetween" function I found here. I also tried to learn the string.Split function. But I don't think I'm getting close to what I want to archive, also I don't really understand splitting a string quite yet.

I basically need to remove all of the " " empty spaces between the values, problem is, the value length may change, e.g "name".

Can someone give me an example how I could extract the values?

Thanks in advance!

Eddd
  • 1
  • 2
  • looks like you can split the second line on space and get the length of each column then you can just use `string.Substring` to pull the values out of each line and use `Trim` to remove the trailing spaces. – juharr Dec 20 '18 at 04:39
  • have a look at this[Efficient function for reading a delimited file into DataTable](https://stackoverflow.com/q/1166272/2417602) – vikscool Dec 20 '18 at 04:39
  • @vikscool This is not a delimited file. It's a fixed width file. – juharr Dec 20 '18 at 04:40
  • Split the whole line by a space `line.Split(' ').Trim()`. I assume the only field that might contain a space as a value (not delimiter) is the name. So you might want to get the splitted string of the first and the last 4 index, and any remaining indexes in the middle are the values of field name. – Michael Yuwono Dec 20 '18 at 04:43
  • Possible duplicate of [Read fixed width record from text file](https://stackoverflow.com/questions/162727/read-fixed-width-record-from-text-file) – Rand Random Dec 20 '18 at 04:58

2 Answers2

1

RegEx.Split is your friend, and this works well enough.

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,}"); 
    }
}
Matt
  • 25,943
  • 66
  • 198
  • 303
1

You can do this with Regex and named groups.

Sample Input

    var str = @"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";

Regex Definition

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);

Parsing Code

var lines = str.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
foreach(var line in lines)
{
    var match = regex.Match(line.Trim());
    if(!match.Success) continue;

    Console.WriteLine($"ID = {match.Groups["id"].Value}");
    Console.WriteLine($"Score = {match.Groups["score"].Value}");
    Console.WriteLine($"Ping = {match.Groups["ping"].Value}");
    Console.WriteLine($"Guid = {match.Groups["guid"].Value}");
    Console.WriteLine($"Name = {match.Groups["name"].Value}");
    Console.WriteLine($"Last Msg = {match.Groups["lastmsg"].Value}");
    Console.WriteLine($"Port = {match.Groups["port"].Value}");
    Console.WriteLine($"Rate = {match.Groups["rate"].Value}");

}

Output

ID = 1
Score = 11
Ping = 45
Guid = 176387877
Name = Player 1
Last Msg = 3250
Port = 3647
Rate = 25000

ID = 2
Score = 23
Ping = 61
Guid = 425716719
Name = Player 2
Last Msg = 3250
Port = 5978
Rate = 25000
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51