简体   繁体   English

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. 我有一个HL7文件,需要进入PID段以获取患者名“ UHCMCDO ^ TWO ^^^^^,然后需要遍历OBR段以获取订单ID 36358,然后提交至数据库表。疲倦地找到PID,然后转到第5个字段以获取患者姓名,但不能。

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. 我知道如何按特定值而不是动态变化的值进行搜索,但是我有100多个文件,我需要获取每个患者的姓名和其Order ID才能生成报告。

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. 其中第一个在PID后面的双管道之间寻找第一个匹配项,第二个在管道之间寻找第二个数字。

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 编辑:这是我在ideone.com( http://ideone.com/0HROlL )上运行的一些代码,使用您的示例作为“原始”字符串

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 . 使用nHapi 在C#中解析HL7的示例 You will find nHapi here or here 您将在这里这里找到nHapi

LINQ based solution - sample usage: 基于LINQ的解决方案-示例用法:

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: 其中Field类是:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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