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

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
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You should be able to split on ". "

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

foreach (string s in Sentences)
{
// do something
  s= s.Trim();
}
0
JElsterAuthor Commented:
There are Periods in the Error and at the end of the sentence
0
käµfm³d 👽Commented:
Based on your previous question, where are you intending on doing the parsing:  on the database server, or in your C#?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Bob BenderCommented:
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
JElsterAuthor Commented:
Parsing in C#
0
JElsterAuthor Commented:
Error X.X  need to act as a delimiter
0
Randy PooleCommented:
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
käµfm³d 👽Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob BenderCommented:
You will learn that there are many ways to skin a cat in programming (accomplish the goal).  

Most all answers will work,
0
Kyle AbrahamsSenior .Net DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.