Solved

string parser

Posted on 2006-11-16
8
198 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 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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