Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Splitting an hl7 message string with C#

Posted on 2007-10-14
11
Medium Priority
?
2,605 Views
Last Modified: 2008-01-09
Hi,
I have an hl7 message string that i'm trying to break apart using regular expressions in C#

The string is very neatly organized, and each segment delimiter is carriage return,  i'm just having trouble splitting the message up. here is a sample message:

ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
NTE|text information text information
ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information

What i would like to do is create a string array of each line from ORC to the next ORC or end of string. so the string above would be broken up as shown below:

ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
NTE|text information text information

ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information

I would really appreciate help on this and thanks in advance for your time.
0
Comment
Question by:amagondes
  • 6
  • 4
11 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20074711
Are you saying that this is a segment with only one carriage return

ORC|text information text informationOBR|text information text informationOBX|text information text informationNTE|text information text informationNTE|text information text information

Or 5 carriage returns

ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
NTE|text information text information
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20074715
Sorry, it's not clear for me:

ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
NTE|text information text information

is one array element or is it array of five elements?

I can't remember exact structure hl7 messages however I believe there can be very few e.g. ORC segments.  Am I right? And if I'm not wrong entire structure is not represented here? So do you want to put just a part of the message into array?
0
 

Author Comment

by:amagondes
ID: 20074779
FernandoSoto,

Thanks for taking a look at this. five carriage returns in the section you showed above

Thanks
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!

 

Author Comment

by:amagondes
ID: 20074783
anarki_jimbel,

Thanks also. The whole entire thing is a single string where segments are separated by carriage returns. Yes this is just a portion of the hl7 message after chopping the first two segments MSH and PID and the last two ZPS segments.

Thanks
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 20074800
OK, have a look at this code. It's not optimized however. On another hand hl7 messages are pretty shors usually, and very high performance isn't critical.

I process the message fragment and split it to three(in this case) array elements as required. Message is hardcoded as you can see:




        private void button1_Click(object sender, EventArgs e)
        {
            string entireMessag = @"SOME HL7 OVERHEAD
ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
NTE|text information text information
ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information
ORC|text information text information
OBR|text information text information
OBX|text information text information
NTE|text information text information"


            IList<int> orcInds = new List<int>();
            int startSearch = 0;

             // indexes for ORC segments
            while (entireMessag.IndexOf("ORC", startSearch) > -1)
            {
                orcInds.Add(entireMessag.IndexOf("ORC", startSearch));
                startSearch = entireMessag.IndexOf("ORC", startSearch) + 1;
            }

            // ORC arrayList

            ArrayList orcSegms = new ArrayList();

            for (int i = 0; i < orcInds.Count; i++)
            {
                int startSeg = orcInds[i];
                int endSeg;
                if (i < orcInds.Count - 1)
                {
                    endSeg = orcInds[i + 1] - 1;
                }
                else
                {
                    endSeg = entireMessag.Length - 1;
                }

                string segment = entireMessag.Substring(startSeg, endSeg + 1 - startSeg);
                orcSegms.Add(segment);

            }

            string[] ORCblocks = (string[])orcSegms.ToArray(typeof(string));

            MessageBox.Show("" + ORCblocks.Length);
        }
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20074805
I don't like to use RegEx if it's not really needed. Sometimes it's just overkill :)
0
 

Author Comment

by:amagondes
ID: 20074813
hi anarki_jimbel,

it looks good except i get this compilation error:

Compiler Error Message: CS0308: The non-generic type 'System.Collections.IList' cannot be used with type arguments

thanks for all yout help
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20074961
Strange, it runs on mine computer...

Did you add "using System.Collections;" in your project?

What line error refers to? Because my IList<int>  is generic - it's strange...
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20074970
Add also

using System.Collections.Generic;
0
 

Author Comment

by:amagondes
ID: 20075520
anarki_jimbel,
Thanks for taking the time to help me with this. I really appreciate it.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 20075748
It's pleasure :)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

581 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