C# parse textual file in a specific format

时间:2017-08-05 11:48:59

标签: c# regex parsing

I have never done something like this so I'm really curious on how this can be performed. I imagine it can be either done via regex or in c# somehow...

I have a textual file with data in following format:

12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password

I have prepared a class which looks like following:

public class ParsedData
(
public string IP { get; set; }
public string Port { get; set; }
public string Username { get; set; }
public string Password { get; set; } 
)

The desired output how I would like it to be is that I can parse each line individually and 1 line should have the data stored in a parsed object (list of ParsedData);

How could do this, and to parse the each line of data individually ?

Can someone help me out ?

var completedList = text.Split(':').Select(pr => new ParsedData 
{ 
 IP = pr.ElementAt(0).ToString() // this should be the IP But I'm getting the 
// Index was outside the bounds of the array. exception in this part
/*My elements here*/

}).ToList(); 

4 个答案:

答案 0 :(得分:2)

It looks like at least one row doesn't have any data in it, maybe there is an empty row in the input data?

Try printing out each row of data before selecting the first element of the array - then you can see which input is causing the exception.

答案 1 :(得分:1)

You may use Regex (.+?):(.+?):(.+?):(.+), here example:

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Main {
    public struct ParsedData {
        public string IP { get; set; }
        public string Port { get; set; }
        public string Username { get; set; }
        public string Password { get; set; } 
    }
    class Prog {
        static List<ParsedData> pdl = new List<ParsedData>();
        static string file = @"12.23.425.56:90:kukur:psiar%4
151.23.255.52:3131:Zandga:Ikurit
52.23.45.56:5125:Ningame:Mirsga!@
112.223.45.56:4000:Bisgo:One0ne";
        static void Main() {
            var re = new Regex(@"(.+?):(.+?):(.+?):(.+)");
            foreach (Match m in re.Matches(file)) {
                pdl.Add(new ParsedData() { IP = m.Groups[1].Value, Port = m.Groups[2].Value, Username = m.Groups[3].Value, Password = m.Groups[4].Value });
                Console.WriteLine("IP: " + m.Groups[1] + " PORT: " + m.Groups[2] + " USR_NM: " + m.Groups[3] + " PASS: " + m.Groups[4]);
            }
        }
    }
}

Also I added an List which contains the data.

答案 2 :(得分:1)

class Program
    {
        static void Main(string[] args)
        {
            //I think you know how to read the file so:
            string text = 
@"12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password";

            List<ParsedData> ps = new List<ParsedData>();

            text.Split(new char[] { '\r','\n' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(c =>
             {
                 var cols = c.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).ToList();

//you can check too if cols have content here

                 ps.Add(new ParsedData()
                 {
                     IP = cols[0]!=null?cols[0]:"", //and check if inside it's content..
                     Port = cols[1],
                     Username = cols[2],
                     Password = cols[3]
                 });
             });

            foreach(ParsedData p in ps)
            {
                Console.WriteLine(p.IP + "\t" + p.Port + "\t" + p.Username + "\t" + p.Password);
            }

        }
    }
    public class ParsedData
    {
        public string IP { get; set; }
        public string Port { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }

答案 3 :(得分:1)

I think you make misunderstood about the pr, it not array now, it the element in the array.

var text = "12.23.45.56:8080:username:password";
var array = text.Split(':');
var data = new ParsedData()
       {
           IP = array[0],
           Port = array[1],
           Username = array[2],
           Password = array[3]
       };