HL7短信,如何查找具体字段信息

本文关键字:字段 信息 查找 短信 何查找 HL7 | 更新日期: 2023-09-27 17:58:11

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

我有一个HL7文件,我需要访问PID段以获得患者名称"UHCMCDO^TWO^^^",然后我需要遍历OBR段以获得订单ID 36358,然后提交到数据库表。我厌倦了查找PID,然后转到第5个字段以获取患者姓名,但无法。

我知道如何按特定值搜索,而不是动态更改值,但我有100个文件,我需要获取每个患者的姓名和他们的订单id来提取报告。

HL7短信,如何查找具体字段信息

如果文件格式始终相同,则可以使用正则表达式来获取要查找的数据。

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

其中第一个是在PID之后寻找双管之间的东西的第一匹配,第二个是在管道之间寻找第二个数字。

如果文件的格式不一致,这将不起作用。

编辑:这是我在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);
    }
}

输出:

UHCMCDO^TWO^^^^
36358

使用nHapi在C#中解析HL7的示例。您可以在此处或此处找到nHapi

基于LINQ的解决方案-示例用法:

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

其中字段类别为:

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