Solved

string parser

Posted on 2006-11-16
8
189 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
  • 4
  • 4
8 Comments
 
LVL 62

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 62

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 48
Trouble with References... 5 22
cs.Designer Issue(2) 2 19
Get String split 5 32
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now