Regular Expressions for Prescription Writing


By Tristan Jones

May 16, 2016

The trick to learning any new programming language well is to come up with a project that touches on a variety of different areas. To teach myself Javascript, I am going to write a pharmacokinetic simulator that will allow the user to enter a medication order and plot a theoretical patient's serum concentration of that medication over time, using a two-compartment model.

The Motivation

The trick to learning any new programming language well is to come up with a project that touches on a variety of different areas. To teach myself Javascript, I am going to write a pharmacokinetic simulator that will allow the user to enter a medication order and plot a theoretical patient's serum concentration of that medication over time, using a two-compartment model.

To accomplish this I will have to learn client-side JS and regular expressions to parse the input, AJAX to fetch drug parameters from the server-side drug database, remind myself of RK4 integration for the actual simulation, and finally use a 3rd party client-side JS/jQuery plotting library. Mission accepted!

About Prescriptions

There are a huge number of different styles of written prescriptions by doctors. One of the most common forms used for in-hospital orders follows the format [Drug Name] [Dose][Units and/or infusion rate] [Route of Administration] [Dosing Interval]. For instance:

  • Ceftriaxone 2g IV q24h
  • Norepinephrine 0.1mcg/kg/min IV
  • Lasix 40mg PO bid
  • Fondaparinux 2.5mg SC q12h

The goal is to output a JSON structure that contains all of the information necessary to construct and run a simple pharmacokinetic simulation. The JSON structure should look like:

{
    "medicationName": "Ceftriaxone",
    "route": "IV",
    "dosing": {
        "isInfusion": false,
        "infusionTimeSeconds": null,
        "isPerKg": false,
        "quantityScalar": 2,
        "quantityUnitMultiplier": 1,
        "intervalSeconds": 86400
    }
}

Alternatively, for an infusion the JSON output will look like:

{
    "medicationName": "Levophed",
    "route": "IV",
    "dosing": {
        "isInfusion": true,
        "infusionTimeSeconds": 60,
        "isPerKg": true,
        "quantityScalar": 0.1,
        "quantityUnitMultiplier": 1E-6,
        "intervalSeconds": null
    }
}

This format works well as it is easy to calculate the number of grams of substance per unit time step simply by multiplying the scalar quantity, unit multiplier, infusion time in seconds, and the time step together.

Breaking Down the Problem

The first step is to split the problem out. Some of the data points are easy to extract, such as the medication name and the route of administration. Others, like the dosage and the dosing interval are more complex. I decided to initially separate the prescription into three parts: the medication, the dose, the route, and the interval. Then a second stage regular expression can be used to further parse the dose and the interval. Of course, if the prescription is for a continuous infusion, then there should be no interval.

The first regular expression (RegEx) is pretty straightforward and just splits the string up by whitespace. In javascript this is easily implemented: '\s' indicates any whitespace character, '\S' matches any non-whitespace character, and '+' means one-or-more-of. Note the '?' (zero-or-one-of) on the end of the dosing interval group - it won't be present if this is an infusion. The '?:' before just prevents a duplicate group being returned with the leading whitespace.

var firstPassRegex = /^(\S+)\s+(\S+)\s+(\S+)(?:\s+(\S+))?$/;

var RxParts = firstPassRegex.exec(prescription);

Now the next step is to parse the

Permalink - Tags: math, biology - Categories: programming, medicine