Solved

string parser

Posted on 2006-11-16
8
209 Views
Last Modified: 2010-04-16
Hi,

I have a file contains some information...here is how it looks like
+++++++++++++++
information here
Data:

LP 1, some name
  1  (Empty)
  2 "***********"
  3 "4713WC908A41"
...
+++++++++++++++
How come I get all the information when I see "Data" until I find "+++++++++++++++"??
Also, I need to parse the information after "Data", like numbers and names.  How can I do so??
0
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17962112
It would help to see the code you are using that is giving you the problem, can you post your code.
0
 

Author Comment

by:xenia27
ID: 17962137
That's the problem.  I am quite confused with how can I parse this file.
Basically, in this file, I read each line to parse "information here" part.
But I have no idea how I can parse "Data" part so it would be easier and correct.  Any suggestion?
Here is my code...

public void ParseRecipeLog(string file, Recipe_Log obj)
{
    string          line;
    string          target;
    string          temp_str;
    int             pos;
    int             index;

    try
    {
        pos = 0;
        index = 0;

        StreamReader reader = new StreamReader(file);
        line = reader.ReadLine();

        while (line != null)
        {

            if (line.Contains("Program Name"))
            {
                target = "Program Name:";
                pos = line.IndexOf(target);
                pos = pos + target.Length + 1;
                obj.recipe_name = line.Substring(pos);
                obj.recipe_name = obj.recipe_name.Trim();
            }
            else if (line.Contains("Data"))
            {
               // here I need to parse more data
            }

            line = reader.ReadLine();
        }
        reader.Close();
    }
    catch (Exception e)
    {
    }
    finally
    {
    }

}
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17962184
You say that the file looks like this:

+++++++++++++++
information here
Data:

LP 1, some name
  1  (Empty)
  2 "***********"
  3 "4713WC908A41"
...
+++++++++++++++

You want to parse the data after the line that has Data: in it.

What marks the end of the data that you want to parse?

How do you want to parse the data?

What version of Visual Studio, 2003 or 2005?

Fernando
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:xenia27
ID: 17962199
I will probably have a struct...

struct Info{
  public int num;
  public string name;
}
and parse those information into Info struct.  
"+++++++++++++" mark will indicate the information I wanna read is finished.
I am using Visual Studio 2005.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17962480
Hi xenia27;

I added code to your sample, see below.

using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

       line = reader.ReadLine();
       while (line != null)
       {
           if (line.Contains("Program Name"))
           {
               target = "Program Name:";
               pos = line.IndexOf(target);
               pos = pos + target.Length + 1;
               obj.recipe_name = line.Substring(pos);
               obj.recipe_name = obj.recipe_name.Trim();
           }
           else if (line.Contains("Data"))
           {
               // here I need to parse more data
               line = reader.ReadLine();
               while (!line.Contains("++++"))
               {
                   if (line != "" )
                   {
                       Info myInfo = new Info();
                       Match m = Regex.Match(line, @"^\w*\s*(\d+),?\s+(.*?)$");
                       myInfo.num = Convert.ToInt32(m.Groups[1].Value);
                       myInfo.name = m.Groups[2].Value.Trim();
                       InfoArray.Add(myInfo);
                   }
                   line = reader.ReadLine();
               }
           }

           line = reader.ReadLine();
       }
       reader.Close();


Fernando
0
 

Author Comment

by:xenia27
ID: 17962753
Thanks for your help...this is exactly what I need.
0
 

Author Comment

by:xenia27
ID: 17962797
one more question...will it remove double quote marks when it processes name part??
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17966023
Hi xenia27;

If you want to remove the double quote marks from then name part as well then replace this line:

                Match m = Regex.Match(line, @"^\w*\s*(\d+),?\s+(.*?)$");

With this line of code.

      Match m = Regex.Match(line, @"^\w*\s*(\d+),?\s+\x22?(.*?)\x22?$",
            RegexOptions.Singleline);

Fernando
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question