web stats
String.replace Vs. java.lang.String().replaceAll( - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 12-28-2011, 09:04 AM
GregD GregD is offline
OBX.3 Kenobi
 
Join Date: May 2010
Posts: 175
GregD is on a distinguished road
Default String.replace Vs. java.lang.String().replaceAll(

I am formatting output in HTML for a PDF Writer destination. The section headers in the OBX segments are always all caps and end with a colon (e.g. RECOMMENDATIONS: or FINDINGS.

I wrote a little code template that would evaluate the line and if it met the criteria of a section header it wraps the text in <b> and </b> and replaces the original text with the new bold formatted text. It worked well until I found that it only replaces the first instance. So if I have FINDINGS: and then later MORE FINDINGS:, only the first FINDINGS: is replaced.

I tried regular expressions, but after several attempts at adding what I thought were the proper slashes and a /g it would not work at all, in that now nothing was being replaced.

This was my original line of code. The var OrigText will contain 'FINDINGS:' and 'BoldText' will contain '<b>FINDINGS:</b>'

PDFOut = PDFOut.toString().replace(OrigText, BoldText);

I changed it to this in an attempt to get a global replace, but now I have a new problem.

PDFOut = java.lang.String(PDFOut).replaceAll(OrigText, BoldText);

This morning I came in and the javascript had throw a number of exceptions on the new line of code. One of the header sections had only an open parenthesis in it and java.lang.string did not like this. The original toString().replace() has no problem with this, but of course, it won't do a replaceAll().

So I'm stumped and I have spent too much time trying to do a global replace in a string with javascript. Can anyone help with this.

The exception on java.lang.string is: Wrapped java.util.regex.PatternSyntaxException: Unclosed group near index 26
Cancer Society (web search

Greg
Reply With Quote
  #2  
Old 01-01-2012, 09:41 PM
nelson.wells nelson.wells is offline
OBX.1 Kenobi
 
Join Date: Sep 2010
Posts: 26
nelson.wells is on a distinguished road
Default

I've done many global replacements in Mirth. Could you post some example code? Not just the call to the function, but the regex definitions as well. It looks like your regex isn't properly formatted based on the error description. Unclosed group means you probably have non-matching parenthesis inside the regex.
Reply With Quote
  #3  
Old 01-03-2012, 09:44 AM
GregD GregD is offline
OBX.3 Kenobi
 
Join Date: May 2010
Posts: 175
GregD is on a distinguished road
Default

Thanks for the reply. I'll look in to this more. There may be something else.

Last edited by GregD; 01-03-2012 at 09:51 AM.
Reply With Quote
  #4  
Old 01-04-2012, 07:10 AM
Bostad Bostad is offline
Level 85 Channel Wizard
 
Join Date: Mar 2009
Location: Stratford Ontario
Posts: 678
Bostad is an unknown quantity at this point
Default

String replace in javascript has stymied me on a number of occasions.

When all else fails this is my go to:

Code:
var strOriginal = new String("Original String");
var strNew = new String("New String");

var pattern = RegExp(strOriginal,'g');  //you can add ,'i' after the global flag if you want case insensitive

var PDFOut = strOriginal.replace(pattern,strNew);

/* WARNING: I've spent the holidays working in arcane languages like CSS and HTML
and did you know they have this thing called Television that you can watch for hours and let your brain go to mush?
What I'm saying is that my syntax may be off. I'm playing catch up and don't have time to run code to test it
*/
__________________
I can be reached through gmail and Google Talk using davidrothbauer at gmail dot com
http://www.linkedin.com/pub/david-rothbauer/5/923/518
hl7coders.wordpress.com

Test all my code suggestions prior to implementation
Reply With Quote
  #5  
Old 01-19-2012, 11:08 AM
GregD GregD is offline
OBX.3 Kenobi
 
Join Date: May 2010
Posts: 175
GregD is on a distinguished road
Default

Thanks for the reply. I still don't know why that one message barfs on the java.lang.String(PDFOut).replaceAll(OrigText, BoldText). I thought it was the open bracket, but could not reproduce it with other similar examples.
Reply With Quote
  #6  
Old 01-19-2012, 11:19 AM
Bostad Bostad is offline
Level 85 Channel Wizard
 
Join Date: Mar 2009
Location: Stratford Ontario
Posts: 678
Bostad is an unknown quantity at this point
Default

Have you tried looking at it in a hex editor? Maybe there's an un-printable character in there somewhere.
__________________
I can be reached through gmail and Google Talk using davidrothbauer at gmail dot com
http://www.linkedin.com/pub/david-rothbauer/5/923/518
hl7coders.wordpress.com

Test all my code suggestions prior to implementation
Reply With Quote
  #7  
Old 01-19-2012, 01:18 PM
GregD GregD is offline
OBX.3 Kenobi
 
Join Date: May 2010
Posts: 175
GregD is on a distinguished road
Default

Actually, I have moved on and lost track of the message. It was just a fluke that one of the few messages I was testing caused that problem. At the time, when trying to figure out what was going on, I took out the one '(' and received no exception. Put it back and it threw the exception.

After posted I tried to duplicate the problem with a simple string containing a '(' and couldn't. I have no idea what was causing it, but it hasn't happened since.

Thanks,

Greg
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Mirth Corporation