replace characters

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

replace characters

Adam Hacking
sorry if this is somewhat dense. I can't seem to get this to work ....
 
I need
 
file_check = "C:/Results.xls"
 
and I get
 
file_check = "C:Results.xls"
 
other permutations result in a error.
 
file_check = replace("C:\Results.xls", "\\", "/");
print(file_check);
   if (File.exists(file_check) == 1) {
  print("Append file");
 }
 else {
  print("Create file");
   }
selectWindow("Log")

Thanks again for all of your help !
 
Adam





Reply | Threaded
Open this post in threaded view
|

Re: replace characters

Michael Schmid
Hi Adam,

you need:
   file_check = replace("C:\\Results.xls", "\\", "/");
   print(file_check);


Michael
________________________________________________________________

On 10 Jun 2009, at 18:44, Adam Hacking wrote:

> sorry if this is somewhat dense. I can't seem to get this to work ....
>
> I need
>
> file_check = "C:/Results.xls"
>
> and I get
>
> file_check = "C:Results.xls"
>
> other permutations result in a error.
>
> file_check = replace("C:\Results.xls", "\\", "/");
> print(file_check);
>    if (File.exists(file_check) == 1) {
>   print("Append file");
>  }
>  else {
>   print("Create file");
>    }
> selectWindow("Log")
>
> Thanks again for all of your help !
>
> Adam
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: replace characters

Norbert Vischer-2
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
Reply | Threaded
Open this post in threaded view
|

Re: replace characters

Michael Schmid
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.html

There 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
>