Search code examples
c#hl7

HL7 text message, how to find the specific field information


MSH|^~\&|A|B|C|D|201402141402||ORM^O01|33987|D|2.3
PID|1|99989392|99989392||UHCMCDO^TWO^^^^||19810101|M|||5678 CANDY CANE LANE^^EUCLID^OH^44117^UNITED STATES||(212)353-6048|(212)323-6078||||99576837||||NonHispan||||||||N
PV1|1|O|320|R|C||49762^Abouassaly^Robert||||||||||||99576837||||||||||Y|||||||||||||||201402141402||||||A49417331
IN1|1|43||MEDICAID-OH: CUYAHOGA COUNTY DEPT OF CHILDREN & FAMILY SERVICES|3955 EUCLID AVE^^CLEVELAND^OH^44115-2505||(216)431-4500|||||||||UHCMCDO^TWO^|S|||||1||||||||||||||123456789001|||||||M
GT1|1||UHCMCDO^TWO^^^||5678 CANDY CANE LANE^^EUCLID^OH^44117|(212) 353-6048||19810101|||S
ORC|NW||||||||20140214140256
OBR|1|36358||GC1^Non GYN - Cytology|R||201403281402||||||||NONGYNC^Non GYN - Cytology|49762^Abouassaly^Robert|||||||||||^^^^^R
DG1|1|I9|V70.0|ROUTINE MEDICAL EXAM - V70.0
OBX|1|TX|PTH_SITE1^Site A|1|left||||||F|||||||
OBX|2|TX|PTH_SPEC1^Specimen A||C-FNA^Fine Needle Aspiration||||||F|||||||

I have a HL7 files which I need to get on to the PID segment to get patient name "UHCMCDO^TWO^^^^ and then I need to traverse to OBR segment to get the Order ID 36358 and then submit to database table. I tired finding the PID then going to 5th field to get the Patient Name but cannot.

I know how to search by specific value, not with dynamic changing values but I have 100s of file and I need to get each patient name and their Order id to pull a report.


Solution

  • If the file format is always the same, you can use regular expressions to get the data you're looking for.

    string name = Regex.Match(inputString,@"^PID([\d\|]*)\|\|(.*?)\|\|").Groups[2].Value;
    string obrId = Regex.Match(inputString,@"OBR\|[\d]*\|(\d*)\|").Groups[1].Value;
    

    where the first is looking for the first match for something between double pipes after PID, and the second is looking for the second number between pipes.

    If the format of the files isn't consistent though, this will not work.

    Edit: Here's a bit of code I ran at ideone.com (http://ideone.com/0HROlL) using your sample as the "raw" string

    using System;
    using System.Text.RegularExpressions;
    
    public class Test
    {
        public static void Main()
        {
            string raw = @""; //Paste text here, new lines and all
            string[] lines = raw.Split(new string[] { Environment.NewLine },  StringSplitOptions.None); 
            string name = "";
            string obrId = "";
            foreach (string line in lines)
            {
                if (line.Contains("PID"))
                {
                    name = Regex.Match(line,@"^PID([\|]*[^\|]*){3}[\|]*([^\|]*)").Groups[2].Value;
                }
                else if (line.Contains("OBR"))
                {
                    obrId = Regex.Match(line,@"OBR\|[\d]*\|(\d*)\|").Groups[1].Value;
                }
            }
            Console.WriteLine(name);
            Console.WriteLine(obrId);
        }
    }
    

    Output:

    UHCMCDO^TWO^^^^
    36358