Login  Register

Re: replace characters

Posted by Norbert Vischer-2 on Jun 11, 2009; 9:50am
URL: http://imagej.273.s1.nabble.com/replace-characters-tp3692181p3692183.html

Hi Adam,

I also had problems with the replace command when I wanted to  
duplicate all backslashes in a path name, eg a\b should become a\\b.  
The  macro below fails with an exception, and I googled a lot of  
comments on "java replace problems". I finally solved the problem by  
processing a character array in a loop.

//this macro fails:

s1 = "a\\b";
print (s1, " len= ", lengthOf(s1));
s2 = replace(s1, "\\", "\\\\");
print (s2, " len= ", lengthOf(s2));


//java.util.regex.PatternSyntaxException: Unexpected internal error  
near index 1
// at java.util.regex.Pattern.error(Pattern.java:1713)
// at java.util.regex.Pattern.compile(Pattern.java:1466)

Norbert Vischer