2

I have a HL7 message that contains one MSH segment and multiple PIV segments.

Using Java REGEX, I need to do these:

1) extract the entire MSH segment

2) find each occurence of PV1 segment.

3) extract the entire content/segments that exists between each PV1 segment - including the PV1 segment.

How do I accomplish the above?

Below is the sample HL7 msg:

MSH|^~\&|OADD|${FACILITY}|HELIX||201012010910||ORU^R01|20101720000042|T|2.2
PID|||PX65^^^MRENTR||Mayer^Ronny
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
OBX|2|NM|K^Potassium^0|1.1|4.5|mmol/L|3.5-5.0||||F|||201012010905|IM^|8035^COX^CATHERINE
(IM)
OBX|3|CE|CL^Chloride^0|1.1|100|mmol/L|98-109||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)

So the final goal is to have a msg like this - to have the extracted MSH segment before each PV1 segment. Then create a new HL7 message object that contains each of such HL7 message. In my example, 3 hl7 message will be created - each ofthem will be passed to HAPI parser.

MSH|^~\&|OADD|${FACILITY}|HELIX||201012010910||ORU^R01|20101720000042|T|2.2
PID|||PX65^^^MRENTR||Mayer^Ronny
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
OBX|2|NM|K^Potassium^0|1.1|4.5|mmol/L|3.5-5.0||||F|||201012010905|IM^|8035^COX^CATHERINE
(IM)
OBX|3|CE|CL^Chloride^0|1.1|100|mmol/L|98-109||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
MSH|^~\&|OADD|${FACILITY}|HELIX||201012010910||ORU^R01|20101720000042|T|2.2
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
MSH|^~\&|OADD|${FACILITY}|HELIX||201012010910||ORU^R01|20101720000042|T|2.2
PV1||I||2|||9898^Jackson^Burt^T|||||||||||IP||||||||||||||||||||||||||201012010840
OBX|1|NM|NA^Sodium^0|1.1|140^3|mmol/L|137-146||||F|||201012010905|IM^|8035^COX^CATHERINE(IM)
Shaeldon
  • 873
  • 4
  • 18
  • 28
Dev
  • 69
  • 7
  • What's your expected output? – Avinash Raj Jan 22 '15 at 01:44
  • My ultimate goal is to add the extracted MSH segment BEFORE each of the PV1 segments. – Dev Jan 22 '15 at 01:48
  • 2
    Doesn't answer the question, but why depend on regexes when [there's a library for parsing HL7 in Java](http://hl7api.sourceforge.net/)? – ig0774 Jan 22 '15 at 04:44
  • 1
    Don't invent a regex parser unless you are building a toy tool or tool to be used only in a safe-predictable-controlled environment ([some further discussion](http://stackoverflow.com/a/25454479/2626313)) – xmojmr Jan 22 '15 at 06:03
  • HAPI parser is throwing an exception because a PV1 needs a MSH. Thus I need to add MSH segement before each PV1 - pass each such HL7 message to HAPI parser. – Dev Jan 22 '15 at 16:25
  • Maybe that's fine for your intended usecase, but the 3 messages you are indicating you want to get out of this manipulation will not be valid HL7 messages: An ORU message with just PV1 and OBX segments is not a valid ORU message, the last 2 don't even have a PID segment? – Emilien Jun 24 '18 at 11:33

1 Answers1

0

You can use the library jar: hapi-base.2.0.jar

With the class ca.uhn.hl7v2.util.Terser you can get the segments that you want of the report.

public Message processMessage(Message theIn) throws ApplicationException,
            HL7Exception {

        theIn = (ORU_R01) new PipeParser(new CanonicalModelClassFactory("2.3"))
                .parse(theIn.encode());

        System.out.println(theIn.printStructure());//for debug better
        Terser menssageParsed = new Terser(theIn);
        String sResp = menssageParsed.get("/.RESPONSE/ORDER_OBSERVATION/OBR-3-1");
}

But if you like to create new segments in the report or edit. You can use this classes

ca.uhn.hl7v2.model.Message; ca.uhn.hl7v2.model.Structure;

Alberto Perez
  • 1,019
  • 15
  • 17