0

I have a message String that separated by different special characters. For example,

MSG|027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD

Now according to user inputs like,

-> 1(1).1 - Means in this string first occurrence of '|'(Occurrence of '~').Occurence of '^' -> O/P: M
-> 1(1) - Means in this string first occurrence of '|'(Occurrence of '~') -> O/P: GUY^M^1^2^MD
-> 1 - Means in this string first occurrence of '|' -> O/P: 027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD

Now inputs can be one of three. Now I have to replace found piece of string (ex. {I: 1(1)} O: GUY^M^1^2^MD) with other string.

Following is piece of code that give only pipe separated regex.

String originalMsg = "MSG|027052^CMXV~MB^M^1^2^MD~GUY^M^1^2^MD|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD";

String msg = originalMsg.replaceAll("^((?:[^|]*\\|){1})[^|]*", "$1ABC" );

O/P: MSG|ABC|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD

EDIT:

String msg = originalMsg.replaceAll("^((?:[^|]*\\|){1}([^~]+.){2}))[^|]*", "$1ABC" );

O/P: MSG|027052^CMXVABC|O|^1^2^STD||||027052^SCHROPP~GUY^M^^^MD

It will only replace '|' separated string. I want one shot strategy that will replace according to user inputs.

0

1 Answer 1

2

Don't try to do this by hand and using regex'es. Use a HL7 parsing library. A good open source library for Java is HAPI. You can parse the HL7 message to an object, where You can access fields and change their values.

Also the syntax for accessing these values does not seem to conform to HL7 standards of addressing message fields. Might want to read up on HL7 standards also a bit. They should be freely available for download from HL7 organisation website

Definetely do remember - never write Your own HL7 parser, You will shoot Yourself in a foot.

Sign up to request clarification or add additional context in comments.

4 Comments

This will be become HL7 version dependent. I need custom(generic) solution.
What do You mean, it will become HL7 version dependent? HAPI supports all major HL7 standard versions, I think, so this shouldn't be an issue. If You mean You will have custom segments, field ordering and datatypes, then I believe HAPI has the possibility of implementing custom message types for this case.
There is no custom parser available in HAPI. May be in future if there is change in format of HL7!!. So I have to think about it also.
I have extended HAPI in one of my own applications to support an additional segment. Doing it on a large scale should also be possible according to this - hl7api.sourceforge.net/conformance.html Anyway I think, that there will be no one über regular expression, that will work for You. You will have to split the message to fields down to the level You need, make a replacement and reassemble it. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.