using System.Collections.Generic;
using System.Text.RegularExpressions;
private static List<string> segments;
public static void SplitHL7Message(string message)
segments = new List<string>(message.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries));
public static string GetField(string accessor)
var accessor_elements = accessor.Split(['.']);
var segmentRepetitionIndex = 0;
var segmentRegex = new Regex(@"(\w+)(?:\[(\d+)\])?");
var segmentRegexMatch = segmentRegex.Match(accessor_elements[0]);
if (segmentRegexMatch.Success)
segmentId = segmentRegexMatch.Groups[1].Value;
segmentRepetitionIndex = segmentRegexMatch.Groups[2].Value != "" ? int.Parse(segmentRegexMatch.Groups[2].Value) - 1 : 0;
accessor_elements = accessor_elements.Skip(1).ToArray();
var foundSegments = segments.FindAll(s => s.StartsWith(segmentId));
if (foundSegments.Count <= segmentRepetitionIndex)
var segment = foundSegments[segmentRepetitionIndex];
var headerSegment = segments[0];
var fieldSeparator = headerSegment[3];
var componentSeparator = headerSegment[4];
var repetitionSeparator = headerSegment[5];
var escapeCharacter = headerSegment[6];
var subcomponentSeparator = headerSegment[7];
var fields = segment.Substring(segmentId.Length + 1).Split(fieldSeparator).ToArray();
fields = [segmentId, $"{fieldSeparator}", $"{componentSeparator}{repetitionSeparator}{escapeCharacter}{subcomponentSeparator}", .. fields.Skip(1).ToArray()];
fields = [segmentId, .. fields];
if (accessor_elements.Length == 0)
var repetitionIndex = -1;
var fieldRegex = new Regex(@"(\d+)(?:\[(\d+)\])?");
var fieldRegexMatch = fieldRegex.Match(accessor_elements[0]);
if (fieldRegexMatch.Success)
fieldIndex = int.Parse(fieldRegexMatch.Groups[1].Value);
repetitionIndex = fieldRegexMatch.Groups[2].Value != "" ? int.Parse(fieldRegexMatch.Groups[2].Value) - 1 : -1;
if (fieldIndex >= fields.Length)
var field = fields[fieldIndex];
if (repetitionIndex >= 0)
var repetitions = field.Split(repetitionSeparator);
if (repetitionIndex >= repetitions.Length)
field = repetitions[repetitionIndex];
if (accessor_elements.Length == 1)
var currentLevel = field;
for (int i = 1; i < accessor_elements.Length; i++)
var currentElement = accessor_elements[i];
var componentIndex = int.Parse(currentElement) - 1;
var components = currentLevel.Split(componentSeparator);
if (componentIndex >= components.Length)
currentLevel = components[componentIndex];
public static void Main(string[] args)
var message = @"MSH|^~\&|TTM||host||20230727170458+0100||SSU^U03^SSU_U03|sQELfcfwyCH2|P|2.5.1|||NE|AL||Unicode UTF-8|||LAB26^IHE~RQPO^ROCHE
EQU|TTM^ROCHE~1234-56^ROCHE|20230727170458|OP
EQU|XXX^ROCHE~5678-61^ROCHE|20240127156789|OP
SAC|||SID||PUUID|||I|HITACHI_917_R^^99ROC|CID|CPOS||||SIO^^99ROC|||||||||||OCAP^^HL70381
SPM|1|SID||“”|||||||P|||||||||||||NCFU^^HL70493|||SCT^^99ROC";
SplitHL7Message(message);
Console.WriteLine(GetField("MSH"));
Console.WriteLine(GetField("MSH.1"));
Console.WriteLine(GetField("MSH.2"));
Console.WriteLine(GetField("MSH.3"));
Console.WriteLine(GetField("MSH.7"));
Console.WriteLine(GetField("MSH.9"));
Console.WriteLine(GetField("MSH.9.2"));
Console.WriteLine(GetField("EQU.1"));
Console.WriteLine(GetField("EQU.1[1].2"));
Console.WriteLine(GetField("EQU.1[1].1"));
Console.WriteLine(GetField("EQU.1[2]"));
Console.WriteLine(GetField("EQU.1.2"));
Console.WriteLine(GetField("SAC.9.3"));
Console.WriteLine(GetField("EQU[2].1"));