aspose file tools*
The moose likes Beginning Java and the fly likes String replace all and File.separator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replace all and File.separator problem" Watch "String replace all and File.separator problem" New topic
Author

String replace all and File.separator problem

Manu Somasekhar
Ranch Hand

Joined: May 22, 2012
Posts: 44
hi,
I just want to make a package name from a dirctory name by replacing all path separators with "."



I am getting PatternSyntaxException Unexpected internal error near index 1
\
^

Any idea what is wrong with it ?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
replaceAll's first argument is a regexp and \ has a special meaning there. The method "replace" might better fit you need.

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Of course it should also work with replaceAll()! You have to escape special characters so that the first parameter is a correct regular expression. To be precise you have to escape the backslash in case of Windows systems. That can be very annoying and verbose because single backslashes aren't allowed in Java Strings either and have to be escaped here, too. But something like the following should work for UNIX and Windows although the backslashes may look strange:

Marco
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
What is the problem? There is a path name and we want to replace every path separator character with ".", the java package separator character.
This intention is adequately expressed with using "replace". Using "replaceAll" is not appropriate here, as there is no regexp logic involved here.
To use "replaceAll" in this situation is not appropriate and its suggestion is bad advice.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

In Java regexes, a backslash is a special character (as also for String literals). Since the value of File.separator on a Windows system is "\" that doesn't fly.

The safe way to handle that is to surround it by start-of-quote and end-of-quote tags. Yo ucan find how to do that in the API for java.util.regex.Pattern. Remember to double the backslashes in that when you assign it from a String literal.


luck, db
There are no new questions, but there may be new answers.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

@Ivan Jozsef Balazs

My intention wasn't to recommend the usage of replaceAll(). Instead I just wanted to explain why Manu's first attempt didn't work as expected. By comparing both solutions it should be obvious that regexps in this case require additional work and make the solution unnecessarily complicated.

Marco
Manu Somasekhar
Ranch Hand

Joined: May 22, 2012
Posts: 44
Thanks Marco,Derryl and Ivan. I got the point. Below code worked for me

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String replace all and File.separator problem