Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes String replaceAll problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replaceAll problem" Watch "String replaceAll problem" New topic
Author

String replaceAll problem

Vadim Vararu
Ranch Hand

Joined: Jan 03, 2009
Posts: 147
Hi everybody. I've got a problem.

I have a string "uploads/sounds" and i try to do that -> "uploads/sounds".replaceAll("/", File.separator); Surprisingly, i get an error. Who knows what's the problem?

If a use something else instead of File.separator as second parameter for replaceAll, it goes allringht.


If you think you've done too much, usually it means you've done too few.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Shouldn't you be using

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Vadim Vararu wrote:
I have a string "uploads/sounds" and i try to do that -> "uploads/sounds".replaceAll("/", File.separator); Surprisingly, i get an error. Who knows what's the problem?


The second parameter to the replaceAll() method is *not* just a regular string. It is a regex replacement string. With a regex replacement string, the backslash has special meaning. So... if you run this with a Windows system, you should get an error, as a single backslash is not valid as a regex replacement string.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


BTW, if you don't want the special meanings to apply to the regex replacement string, you can also use the Matcher.quoteReplacement() method to quote out the special meaning.

Or you can just not used regexes parameters. as you can use the replace(String, String) method instead.

Henry
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Henry Wong wrote:
Vadim Vararu wrote:
I have a string "uploads/sounds" and i try to do that -> "uploads/sounds".replaceAll("/", File.separator); Surprisingly, i get an error. Who knows what's the problem?


The second parameter to the replaceAll() method is *not* just a regular string. It is a regex replacement string.


Correct: the second parameter is not an ordinary String, but it's not a regex String. The only thing special with it, is that variables are interpolated in it. In other words, the dollar sign is the only special character in the second parameter. The dollar sign followed by a number denotes the N-th match from the regex pattern (from the first parameter).

Henry Wong wrote:With a regex replacement string, the backslash has special meaning. So... if you run this with a Windows system, you should get an error, as a single backslash is not valid as a regex replacement string.

Henry


But, regardless of OS (*nix, Mac, Windows), all paths with a slash as separator will work just fine.
So, OP: I'd just keep using (relative) path names with the '/', also on Windows.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Correct: the second parameter is not an ordinary String, but it's not a regex String. The only thing special with it, is that variables are interpolated in it. In other words, the dollar sign is the only special character in the second parameter. The dollar sign followed by a number denotes the N-th match from the regex pattern (from the first parameter).


I never said that the second parameter was a regex string -- I said the second parameter was a regex replacement string.

And the dollar sign is not the only character that has special meaning. The backslash is also special. It is needed if you want to actually replace with a dollar sign (or a backslash).

Henry
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Henry Wong wrote:
Correct: the second parameter is not an ordinary String, but it's not a regex String. The only thing special with it, is that variables are interpolated in it. In other words, the dollar sign is the only special character in the second parameter. The dollar sign followed by a number denotes the N-th match from the regex pattern (from the first parameter).


I never said that the second parameter was a regex string -- I said the second parameter was a regex replacement string.

...


Ah, I misunderstood your post.

Henry Wong wrote:The backslash is also special.


Of course!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String replaceAll problem