Sidebar

How can I globally replace a special character in my message?

+1 vote
1.5K views
asked Jul 2, 2014 by matt-w-2627 (3,220 points)
A lab vendor is sending us a bulk HL7 lab message, but instead of ^ (hat) component separators, they are using a special character. Normally, it wouldnt matter which separator they use, as long as they declare it in the MSH field, but this character is getting translated poorly during transmission.

I tried to use the standard StringUtils.replace on it, but it doesn't seem to be having any effect

1 Answer

+3 votes
 
Best answer

In order to understand why StringUtils.replace doesnt appear to be working, we need to understand which character you are trying to replace.

In this example, the special character is µ (Mu or Micro)

if you were to try and use this in StringUltils.replace:

message.setNode('/', StringUtils.replace(message, 'µ', '^'));

It likely will not succeed, because there are many hex or unicode characters that result in that symbol. You have to make sure you have the right one.

If you open the message in a hex editor (such as ultra edit or HxD), you might find that the Hex equivalent for your special character is something like B5

you can convert that Hex Code into a Unicode equivalent (http://www.fileformat.info/info/unicode/char/b5/index.htm) and use it in StringUtils.replace along with the escape character:

message.setNode('/', StringUtils.replace(message, '\u00B5', '^'));

below is a code snippet for a good way to test this process out in QIE:

//first, get a line from the sample to test with
var mshLine = message.getNode('MSH');
qie.info("before = " + mshLine);
//check if the character exists (indexOf will be greater than -1)
qie.info("index of \u00B5 = " + StringUtils.indexOf(mshLine, '\u00B5', 0));
//attempt the replace
mshLine = StringUtils.replace(mshLine, '\u00B5', '^');
qie.info("after = " + mshLine);
//check if the character exists after the replacement (indexOf should be -1)
qie.info("index of \u00B5 = " + StringUtils.indexOf(mshLine, '\u00B5', 0));

 

 

 

answered Jul 2, 2014 by matt-w-2627 (3,220 points)
selected Jul 2, 2014 by mike-r-7535
commented Jul 2, 2014 by mike-r-7535 (13,830 points)
The one caveat with replacing the special character, is that any existing ^ symbols in your HL7 need to be changed to /S/, and all previous /S/ need to be changed to Mu.  See http://www.hl7standards.com/blog/2006/11/02/hl7-escape-sequences/ for more information on HL7 escape characters.
...