Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string parser

Posted on 2006-11-16
8
Medium Priority
?
220 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 64

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 64

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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