Solved

string parser

Posted on 2006-11-16
8
190 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

20 Experts available now in Live!

Get 1:1 Help Now