• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replaceAll problem

 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't you be using

 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic