0

I am reading data from a vendor via an ftpwebrequest in c#. Thus the data will be in a string format line by line e.g

0123030014030300123003120312030203013003104234923942348

I need to parse this data into the appropriate fields so I can then insert them into a sql talble. I know the position each field starts at so I want to use regex to parse each field. This is the function I use to get the data now I just need to parse the data. I am haveing trouble finding a clear solution on how to do so. Any suggestions would be greatly appreciated :)

    static void GetData()
    {
        WebClient request = new WebClient();
        string url = "ftp://ftp.WebSite.com/file";
        request.Credentials = new NetworkCredential("userid", "password");
        try
        {
            byte[] newFileData = request.DownloadData(url);
            string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
            Console.WriteLine(fileString);
        }
        catch (WebException e)
        {

        } 
    }
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Ryder
  • 281
  • 1
  • 4
  • 5
  • 2
    Well, not too sure why you would use regex for this. I would use Substring my self. – CrazyDart Jun 26 '12 at 19:42
  • 1
    What is the pattern for the data to go into the table? – ΩmegaMan Jun 26 '12 at 19:43
  • The whole thing will be in a string, so figure out what delimits a new line and do a split on the string, then you will have an array of string, you can then pull the data out using subscting because you know the start of each bit of data. – CrazyDart Jun 26 '12 at 19:46
  • @CrazyDart - agreed, this does make the most sense now, I have never used regex and was not sure if there was a way to do so. Thanks! – Ryder Jun 26 '12 at 19:52

2 Answers2

1

You don't need to use a regular expression if you're just splitting at specified lengths. In C# you can just use String.Substring, for example:

byte[] newFileData = request.DownloadData(url);
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
string fieldOne = fileString.Substring(0, n);
Tyson
  • 1,685
  • 15
  • 36
0

Here is an example of regex. We specify what to capture by the totals of \d{x} such as \d{4} gives us 4 digits. Then we place it into a named capture group (?<NameHere>\d{4}) then we access it by its name (NameHere in previous text example) for the processing.

See example with removal of three fields Id, group # and Target numbers:

string data = "0123030014030300123003120312030203013003104234923942348";

string pattern = @"^(?<ID>\d{4})(?<Group>\d{3})(?<Target>\d{8})";

Match mt = Regex.Match(data, pattern);

// Writes
// ID: 0123 of Group 030 on Target: 01403030
Console.WriteLine("ID: {0} of Group {1} on Target: {2}", mt.Groups["ID"].Value, mt.Groups["Group"].Value, mt.Groups["Target"].Value);
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122