Skip to content
The Path

Module 2 · Reading an HL7 v2 message·10 min

Delimiters and escape sequences

The five characters that split everything, and how to write a “|” inside text.

At the sleep lab, a technologist types a free-text note: “nasal mask & humidifier”. Harmless to a human. To an HL7 message, that & is a separator: without care, the receiver cuts the sentence in two. This whole lesson answers one question: how do you write a reserved character inside data?

Here is a polysomnography result for Marie Dubois, with a technologist note in the NTE segment.

The sleep lab sends Marie Dubois’s AHI, followed by a technologist comment that contains a vertical bar and an ampersand.

ORU^R01^ORU_R01· Observation result (unsolicited)
||||||||||
||||||||
||||
|||||||||||
|||

The tool shows the raw value, exactly as it travels over the wire. The receiving system decodes it before displaying it.

The five delimiters

You met them in the message anatomy lesson. Here is their full ID card, with where each one lives in the header.

Delimiter Suggested value Where it is declared Role
Field separator | 4th character of the segment, MSH-1 separates fields
Component separator ^ 1st character of MSH-2 splits a field
Repetition separator ~ 2nd character of MSH-2 separates occurrences of a field
Escape character \ 3rd character of MSH-2 opens and closes an escape sequence
Subcomponent separator & 4th character of MSH-2 splits a component

The values are suggested, not mandatory. Nearly everyone uses |^~\&, but a well-behaved receiver always reads the delimiters from the header of the message it gets.

MSH-1 and MSH-2: the message states its own rules

The MSH segment starts with three letters, then one character. That fourth character is the field separator: MSH-1. Right after it come four characters in a fixed order: component, repetition, escape, subcomponent. That is MSH-2.

The delimiters declared in MSH apply to the whole message. The receiver reads them first, then uses them to split everything else.

The segment terminator: carriage return

One delimiter is missing from the table: the one that ends each segment. It is always a carriage return, ASCII character 13 (hex 0D). It is not declared in MSH, and the standard forbids changing it.

On screen, each segment shows on its own line. Under the hood, what separates those lines is that one invisible character.

Escape sequences

An escape sequence has three parts: the escape character, a one-letter code, then the escape character again. With \ as the escape character, you get:

Sequence Stands for Character
\F\ the Field separator |
\S\ the component Separator ^
\T\ the subcomponenT separator &
\R\ the Repetition separator ~
\E\ the Escape character itself \

The sender replaces every reserved character found in the data with its sequence. The receiver does the reverse after splitting the message. Sequences are never nested.

These five sequences are allowed in text fields: ST (short string), TX (long text) and FT (formatted text). They are also allowed in the data component of the ED type, the one that carries an encoded document such as a PDF.

Decoding Marie Dubois’s note

Take the technologist note as it travels:

Supine AHI \F\ lateral: 41 \F\ 12\.br\Nasal mask \T\ humidifier advised

The receiver displays:

Supine AHI | lateral: 41 | 12
Nasal mask & humidifier advised

Each \F\ turns back into a bar, the \T\ turns back into an ampersand. The \.br\ is a formatting command, covered just below.

Now break it yourself. In the editor, replace the first \F\ with a real | bar. Then click the end of the note: it has moved into NTE-4, a field that expects a code, not free text.

The same note, editable. Replace a sequence with the raw character and watch where the text lands.

ORU^R01^ORU_R01· Observation result (unsolicited)
||||||||||
||||||||
|||

The other, rarer sequences

Sequence Effect Fields
\H\ then \N\ start then end of highlighting; the receiver picks the style (bold, color) TX, FT
\.br\ starts a new line FT
\Xdd\ bytes written in hexadecimal, in pairs of digits TX, FT; the meaning is agreed between the two systems

NTE-3, the comment field, is of type FT. That is why the \.br\ in the note is valid there. FT has other formatting commands too: \.sp\, \.in\, \.ti\, \.ce\, \.fi\, \.nf\.

Going further: \Cxxyy\ and \Mxxyyzz\ switch character sets in the middle of a text, and \Zdd\ is reserved for local sequences. You will rarely meet them; they go hand in hand with MSH-18, the character set, covered in detail in the “Character sets and accents” lesson.

And in FHIR?

FHIR travels as JSON or XML. Those formats have their own rules for special characters, handled by any standard library. Sequences like \F\ or \T\ do not exist in FHIR: a tool converting v2 to FHIR has to decode them first.

Check your understanding

1 of 4

A technologist types “nasal mask & humidifier” into a comment. How must that & travel inside the message?

+50 XPPass the quiz (3 correct answers) to complete the lesson.

Reviewed and checked on 28 September 2026