Insights

HL7 Escape Sequences and Escape Characters: The Complete List

HL7 v2 uses a handful of special characters to structure every message: | separates fields, ^ separates components, ~ separates repetitions, & separates subcomponents and \ marks an escape. So what happens when a patient's address, a lab comment or a clinician's note actually contains one of those characters? Without escaping, the receiver splits the data in the wrong place and the message is corrupted.

Escape sequences solve this. This guide lists every common HL7 escape sequence, shows examples and explains the mistakes that cause garbled data in production.

How escape sequences work

An escape sequence starts and ends with the escape character defined in MSH-2, which is almost always the backslash \. Between the two backslashes is a code that tells the receiver what to put back. For example, \F\ means "a literal field separator goes here".

The escape character is read from MSH-2, not assumed. If a sender declares different encoding characters, the escape sequences use that sender's escape character instead.

The delimiter escape sequences

These five are by far the most common:

\F\Field separator`\`
\S\Component separator^
\T\Subcomponent separator&
\R\Repetition separator~
\E\Escape character\

Version 2.7 and later also define \P\ for the truncation character (# by default).

Example

A lab comment that should read:

Result A|B, see note ^ref & retest

must be sent as:

OBX|1|TX|COMMENT||Result A\F\B, see note \S\ref \T\ retest||||||F

A compliant receiver turns each sequence back into the original character after parsing. Paste the line into the free AckHarbor™ HL7 Message Viewer to see how the field is split.

Formatting escape sequences

For formatted text data (the FT data type), HL7 defines formatting commands. The most widely used is the line break:

\.br\Line break
\.sp n\Skip n lines (a blank line if n is omitted)
\.in n\Indent n spaces
\.ti n\Temporarily indent n spaces
\.ce\Centre the next line
\.fi\Begin word wrap (fill mode)
\.nf\End word wrap (no-fill mode)
\.sk n\Skip n spaces

Many systems also accept \.br\ in TX fields, but that depends on the receiver. Check the interface specification rather than assuming.

Highlighting and character-set sequences

\H\Start highlighting
\N\Normal text (end highlighting)
\Xdddd...\Hexadecimal data, two hex digits per byte
\Zdddd...\Locally defined escape sequence
\Cxxyy\Single-byte character set switch (ISO 2022)
\Mxxyyzz\Multi-byte character set switch (ISO 2022)

\X0D\ is often seen for a carriage return and \X0A\ for a line feed. Remember that a raw carriage return inside a field ends the segment in HL7 v2, so line breaks inside text must always be escaped.

Common escaping mistakes

  1. Not escaping at all. A free-text comment containing | shifts every following field one position to the right.
  2. Double escaping. An interface engine escapes data that was already escaped, so the user sees \E\F\E\ instead of |.
  3. Escaping the MSH-2 field. The encoding characters in MSH-2 are never escaped. They define the escape rules.
  4. Raw line breaks. Sending carriage returns inside OBX-5 splits the segment and the remaining text is parsed as a broken segment.
  5. Assuming the backslash. Always read the escape character from MSH-2.
  6. Unescaping in the wrong order. Unescape after splitting the message into fields and components, never before. Otherwise the restored delimiters are treated as real delimiters.

How to test escaping safely

Use de-identified or synthetic data only. The free AckHarbor™ HL7 De-identify tool can strip patient details from a sample before you share it with a vendor or colleague. Then paste the message into the HL7 Message Viewer to confirm each field lands where you expect.

Get the free Quick Reference

Keep the full escape-sequence table on your desk. Download the free AckHarbor™ HL7 Quick Reference at ackharbor.com/quick-reference/, which also covers delimiters, key segments and ACK codes.

FAQ

What is the escape character in HL7?

By default it is the backslash \, the third character of MSH-2 (^~\&). The actual value is whatever the sender declares in MSH-2.

How do I send a pipe character in an HL7 field?

Replace it with \F\. The receiver converts it back to | after parsing.

How do I add a line break in an HL7 message?

Use \.br\ in formatted text fields, or \X0D\ or \X0A\ where the receiver supports hex escapes. Never send a raw carriage return inside a field.

What does \E\ mean in HL7?

It represents a literal escape character, so a backslash inside data is sent as \E\.

Do FHIR resources use HL7 v2 escape sequences?

No. FHIR uses JSON or XML, which have their own escaping rules. HL7 v2 escape sequences must be decoded when mapping v2 data to FHIR.

---

*HL7® and FHIR® are registered trademarks of Health Level Seven International.*

Keep practicing — free HL7 viewer and FHIR viewer with Insights, or the Sandbox.
Academy previewFree Sandbox

Related: HL7 viewer · FHIR JSON viewer · segment reference · free Quick Reference PDF