简体   繁体   中英

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.

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

An example for HL7 parsing in C# with nHapi . You will find nHapi here or here

LINQ based solution - sample usage:

var fields = Field.Parse(hl7);
var name = fields.First(Field.Locate("PID:5")).Value;
var order = fields.First(Field.Locate("OBR:2")).Value;

where Field class is:

public class Field
{
    public int SegmentSequence { get; set; }
    public string SegmentId { get; set; }
    public int FieldIndex { get; set; }
    public string Value { get; set; }

    public static IList<Field> Parse(string hl7, string segmentDelimiter = "\r")
    {
        if(hl7 == null) throw new ArgumentNullException("hl7");
        if(hl7.Length < 4) throw new ArgumentException("Invalid HL7 syntax.");
        hl7 = hl7.Replace("\r\n", "\r");
        try
        {
            var fieldDelimiter = hl7[3];
            return hl7.Split(new string[] { segmentDelimiter }, StringSplitOptions.None)
                .Where (s => s.Length > 0)
                .SelectMany(
                    (s, i) => s.Split(fieldDelimiter)
                                .Select(
                                    (f, j) => new Field { 
                                                    SegmentSequence = i, 
                                                    SegmentId = s.Substring(0,3), 
                                                    FieldIndex = i==0 ? j+1 : j, 
                                                    Value = f
                                                }
                                        )
                ).Where(o => !(o.FieldIndex == 0) && !(o.SegmentSequence==0 && o.FieldIndex==1))
                .ToList();
        }
        catch
        {
            throw new ArgumentException("Invalid HL7 syntax.");
        }
    }

    public static Func<Field, bool> Locate(string descriptor)
    {
        if(descriptor == null) throw new ArgumentNullException(descriptor);
        Action throwSyntaxException = () => {
            var msg = string.Format("Invalid descriptor syntax: '{0}'", descriptor);
            throw new InvalidOperationException(msg);
        };

        var elements = descriptor.Split(':');
        if(elements.Length != 2) throwSyntaxException();

        int ndx;
        if(!int.TryParse(elements[1], out ndx)) throwSyntaxException();

        return (field) => field.SegmentId == elements[0] && field.FieldIndex == ndx;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM