[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# - Help parsing out 'sentences' in comments field

Posted on 2014-07-14
10
Medium Priority
?
155 Views
Last Modified: 2014-07-15
I have a comments field with data that looks like this.  I Need parse out the 'Sentences' that begin with Error X.X and end with the next Error X.X.

"Error 1.0  Data Error has occured.         Error 9  I/O Error Disk on fire.         Error 3.4  Computer exploded
Error 9.9 Total melt down of server. Error 10.0 User Error"

So.. I would need 5 new records with 1 error each.

thx in advance
0
Comment
Question by:JElster
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 40195723
You should be able to split on ". "

string[] Sentences = myString.Split(". ");

foreach (string s in Sentences)
{
// do something
  s= s.Trim();
}
0
 
LVL 1

Author Comment

by:JElster
ID: 40195747
There are Periods in the Error and at the end of the sentence
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40195748
Based on your previous question, where are you intending on doing the parsing:  on the database server, or in your C#?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Bob Bender
ID: 40195763
Author.......There are Periods in the Error and at the end of the sentence

Yes it is true that a period is in both places, but take note of the Kyles comment...  You should be able to split on ". "

It is a "Period and a space".  Not just a period.  

So, the one in the error number will NOT match and sentence will be split correctly.

Course, you would also want to TRIM leading spaces too, so that the word Error was the fist part, not a bunch of spaces.

Another thought or possibility, (in my mind) is that there is a TAB character after the period.  Not sure what that would do

Bob
0
 
LVL 1

Author Comment

by:JElster
ID: 40195772
Parsing in C#
0
 
LVL 1

Author Comment

by:JElster
ID: 40195775
Error X.X  need to act as a delimiter
0
 
LVL 21

Assisted Solution

by:Randy Poole
Randy Poole earned 800 total points
ID: 40195794
You can try something like this:
string ln="Error 1.0  Data Error has occured.         Error 9  I/O Error Disk on fire.         Error 3.4  Computer exploded Error 9.9 Total melt down of server. Error 10.0 User Error";
            string[] lns;
            List<string> op=new List<string>();
            string cl="";
            string s;
            int c;
            int p;
            lns= ln.Split(new string[]{"Error "},System.StringSplitOptions.None);
            for (c = 0; c < lns.Length; c++)
            {
                s = lns[c].Trim();
                if (s.Length > 0)
                {
                    if (int.TryParse(s.Substring(0, 1), out p))
                    {
                        if (cl!="") op.Add(cl);
                        cl = s;
                        if (c == lns.Length - 1) op.Add(cl);
                    }
                    else
                    {
                        cl += " Error " + s;
                    }
                }
            }
            foreach (string s1 in op)
            {
                Debug.WriteLine(s1);
            }

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 880 total points
ID: 40195847
You could use regex for this task:

string s = "Error 1.0  Data Error has occured.         Error 9  I/O Error Disk on fire.         Error 3.4  Computer exploded Error 9.9 Total melt down of server. Error 10.0 User Error";

MatchCollection matches = Regex.Matches(s, @"Error +[0-9]+(?:\.[0-9]+)?(?:.(?!Error +[0-9]+(?:\.[0-9]+)?))+");

foreach (Match m in matches)
{
    Console.WriteLine(m.Value.Trim());
}

Open in new window

0
 
LVL 5

Expert Comment

by:Bob Bender
ID: 40196108
You will learn that there are many ways to skin a cat in programming (accomplish the goal).  

Most all answers will work,
0
 
LVL 41

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 300 total points
ID: 40197052
Is there any reason why Error X.X needs to be a delimiter?

Note that there's also a regex.Split as well, and  based on kaufmed's answer in your previous question:

string regEx = "Error *[0-9]+(?:\.[0-9]+)?";
string[] sentences = Regex.Split(errorString, regEx);



Thanks to Bob for clarifying my original answer.  There are definitely more than one solutions to a goal.  Some are more efficient than others:

1+1+1 = 3
 2+1 = 3
 1+2 = 3
   3 = 3

All valid.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

873 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