Introduction to the Logs

Regex Search Criteria

You can use also use regular expressions (regex) in the Log Text Query search field, simply enclose the expression in slash characters /<regex>/.
Note: The closing slash is optional unless you wish to combine multiple regex expressions.

Tip: I found the Regex 101 online tester very helpful when creating/testing the expressions on this page. The Regex Buddy application (Windows only) also comes highly recommended, though it is not free. And you can google to find many other regex testers.

Here are a few ideas for (hopefully) useful things you can do with regex queries:

  1. Match all HL7 messages for all channels
    • Use an anchor “^” to match all channels beginning with “MSH”:
      /^MSH/

      Alternatively you can omit the second slash:

      /^MSH
    • This query will return all log entries starting with MSH, to restrict it to messages select “Messages” from the Type list in the Search Criteria panel.
  2. Match non-HL7 messages for all channels:
    • First select “Messages” from the Type list in the Search Criteria panel.
    • Then negate the regex (from above) that matches all HL7 messages:
      NOT /^MSH/
    • Alternatively use a regex with look-ahead not equals “?!”:
      /^(?!MSH)/
  3. Match all messages for all patients with the same surname, for a specified channel.
    • Select “Messages” from the Type list in the Search Criteria panel.
    • Select the the desired channel name from Channel drop down (we used Sender).
    • This regex returns all records containing the name, unfortunately it matches relatives (NK1) and doctors (PV1) named “Smith” as well as Patients (PID):
      Note: A simple (non-regex) search (without the slashes) will give the same result.

      /Smith/
    • This regex checks the Name field in the PID segment so it only matches only matches patients named “Smith”:
      /PID(\|[^|]*){5}Smith/
    • Check for alternative spelling of Smith, Smithe, Smyth, or Smythe:
      /PID(\|[^|]*){5}(Sm[iy]the?)\b/
    • You can also check for multiple names:
      /PID(\|[^|]*){5}(Smith|Jones)/
  4. Exclude all messages for all patients with the same surname, for a specified channel.
    • Select “Messages” from the Type list in the Search Criteria panel.
    • Select the the desired channel name from Channel drop down (we used Sender).
    • Negating the matching regex excludes all records containing the name, unfortunately it excludes relatives (NK1) and doctors (PV1) named “Smith” as well as Patients (PID):
      Note: A simple (non-regex) search (without the slashes) will give the same result.

      NOT /Smith/

      Alternatively use regex with look-ahead not equals “?!”, unfortunately this also excludes all messages containing “Smith” not just patients:

      /^(?!.*Smith)/
    • Negating a regex that checks the Name field in the PID segment excludes patients named “Smith”:
      NOT /PID(\|[^|]*){5}Smith/

      Alternatively use regex with look-ahead not equals “?!”:

      /PID(\|[^|]*){4}\|(?!Smith\b)/
    • Negate checking for alternative spelling of Smith, Smyth, or Smythe:
      NOT /PID(\|[^|]*){5}(Sm[iy]the?)/

      Alternatively use regex with look-ahead not equals “?!”:

      /PID(\|[^|]*){4}\|(?!Sm[iy]the?)/

      Note: This regex relies checking the first (surname) sub-field in the name field, by using “\|” (/PID(\|[^|]*){4}\|(?!Sm[i|y]th[e]?)/) to match the start of the name field.

    • You can also negate checking for multiple names:names:
      NOT /PID(\|[^|]*){5}(Smith\b|Jones)/

      Alternatively use regex with look-ahead not equals “?!”:

      /PID(\|[^|]*){4}\|(?!Smith\b|Jones)/
  5. Match a specified value in a specified field in an HL7 message.
    • Use this regex format:
      /<segment>(\|[^|]*){<field>}<value>/
    • To find Toronto in the address field of the PID segment:
      /PID(\|[^|]*){11}Toronto/
    • To find LA or Los Angeles in the address field of the PID segment:
      Note: The “\b” (word boundary) to only match “LA” as a whole word, to prevent matching things like “Miller Lane” or “Lake Rd”

      /PID(\|[^|]*){11}(Los Angeles|LA\b)/
  6. Exclude a specified value in a specified field in an HL7 message.
    • Negate this regex format:
      NOT /<segment>(\|[^|]*){<field>}<value>/
    • To exclude Toronto in the address field of the PID segment:
      NOT /PID(\|[^|]*){11}Toronto/
    • To exclude LA or Los Angeles in the address field of the PID segment:
      Note: The “\b” (word boundary) to only match “LA” as a whole word, to prevent matching things like “Miller Lane” or “Lake Rd”

      NOT /PID(\|[^|]*){11}(Los Angeles|LA\b)/
    • We didn’t come up with a regex to exclude values in a field (let us know if you figure it out). However we were able to exclude values in a sub-field (see point 8 below).
  7. Match a specified value in a specified sub-field in an HL7 message.
    • Use this regex format:
      /<segment>((\|[^|]*){<previous-field>}\|([^^]*\^){<sub-field>})<value>/
    • To find Toronto in the address field of the PID segment:
      /PID((\|[^|]*){10}\|([^^]*\^){2})Toronto/
    • To find LA or Los Angeles in the address field of the PID segment:
      Note: A “\b” (word boundary) to match “LA” only is not required, because we are now only searching the specific sub-field.

      /PID((\|[^|]*){10}\|([^^]*\^){2})(Los Angeles|LA)/
    • To find the name Smith, Smyth, or Smythe in the surname sub-field:
      Note: A “\b” (word boundary) is not required because we are only searching the specific sub-field.

      /PID((\|[^|]*){4}\|([^^]*\^){0})(Sm[iy]the?)/
  8. Exclude a specified value in a specified sub-field in an HL7 message.
    • Use this regex format:
      /<segment>((\|[^|]*){<previous-field>}\|([^^]*\^){<sub-field>})[^(<value>)]/
    • To exclude Toronto in the address field of the PID segment:
      /PID((\|[^|]*){10}\|([^^]*\^){2})[^(Toronto)]/
    • To exclude LA or Los Angeles in the address field of the PID segment:
      Note: A “\b” (word boundary) to match “LA” only is not required, because we are now only searching the specific sub-field.

      /PID((\|[^|]*){10}\|([^^]*\^){2})[^(Los Angeles|LA)]/
    • To exclude the name Smith, Smyth, or Smythe in the surname sub-field:
      Note: A “\b” (word boundary) is not required because we are only searching the specific sub-field, also we used “(i|y)” instead of the bracketed character class “[iy]” we used earlier (because it raises an error when used with character class negation [^…]).

      /PID((\|[^|]*){4}\|([^^]*\^){0})[^(Sm(i|y)the?)]/
  9. Match or exclude messages where the patient surname matches the surname of a relative.
    1. Use this regex to match:
      Note: “(?s)” is a PCRE option to match newlines (\n), “\d” matches a single numerical digit, “\3” represents the 3rd capture group (that captures the surname).

      /(?s)PID((\|[^|]*){4}\|([^^]*[\^|\|]){1})(.*NK1\|\d\|\3)/

      Alternatively using a named group can be easier to understand:
      Note: “?P<surname>” names the group, “?P=surname” references it, also we used “?:” to make other groups non-capturing (more efficient).

      (?s)PID(?:(?:\|[^|]*){4}\|(?P<surname>[^^]*[\^|\|]){1})(.*NK1\|\d\|(?P=surname))
    2. Use a regex with look-ahead not equals “?!” to exclude:
      /(?s)PID(?:(?:\|[^|]*){4}\|(?P<surname>[^^]*[\^|\|]){1})(?!.*NK1\|\d\|(?P=surname))/
  10. Match or exclude messages where the patient surname matches the surname of a (referring) doctor.
    • Use this regex to match:
      Note: “(?s)” is a PCRE option to match newlines (\n), “\d” matches a single numerical digit, “\3” represents the 3rd capture group (that captures the surname).

      /(?s)PID(?:(?:\|[^|]*){4}\|(?P<surname>[^^]*[\^|\|]){1})(.*PV1.*[\d]*\^(?P=surname))/

      Note: This regex assumes that the Person Identifier code for the Doctor contains a numeric code, or if the code is alpha, the regex will not match.

    • Use a regex with look-ahead not equals “?!” to exclude:
      /(?s)PID(?:(?:\|[^|]*){4}\|(?P<surname>[^^]*[\^|\|]){1})(?!.*PV1.*[\d]*\^(?P=surname))/