Hi Norbert,
this is a funny case...
From the macro documentation:
If old or new are longer than one character, each substring of string that
matches the regular expression old is replaced with new.
Your replacement string has 2 characters, so you need regular expressions;
in this case the relevant web page is
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlThere you see that you need two backslashes in the string to match a
single backslash as a regular expression.
Under quotes, you have to write two backslashes to get a single one into a
string.
This gives 4(!) backslashes that you have to write under quotes to match a
single backslash:
s1 = "a\\b";
print (s1, " len1= ", lengthOf(s1));
s2 = replace(s1, "\\\\", "\\\\\\\\");
print (s2, " len2= ", lengthOf(s2));
Print output:
a\b len1= 3
a\\b len2= 4
Michael
___________________________________________________________________
On Thu, June 11, 2009 11:50, Norbert Vischer wrote:
> 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
>