String.replaceAll troubles with regEx

K

Knute Johnson

O

Oliver Wong

I'm trying to replace instances of a single quote "'" with a backslash
single quote "\'".

But I can't get it to work with the replaceAll function.
http://java.sun.com/j2se/1.4.2/docs...eplaceAll(java.lang.String, java.lang.String)

I'm trying String.replaceAll("'","\\'")

What am I doing wrong? Even when I put a "C" in the first argument, it
only replaces it a single quote. Not a backslash and then a single
quote.

You need something like replaceAll("'","\\\\'"), with 4 blackslashes.
There's two layers of escaping going on: One in Java, and one in the RegExp
engine. See
http://groups.google.com/group/comp.lang.java.programmer/msg/3fd11f7fb586e837
for an explanation. When you write "\\\\'" in Java, the in-memory string
becomes "\\'", and then the RegExp engine treats that as "\'".


- Oliver
 
C

Chris Smith

Oliver Wong said:
You need something like replaceAll("'","\\\\'"), with 4 blackslashes.
There's two layers of escaping going on: One in Java, and one in the RegExp
engine.

The subtlety worth pointing out here is that even the second parameter
to String.replaceAll is NOT a plain String. The API docs for
String.replaceAll are far from clear in pointing this out. It is
pointed out in the API docs for Matcher.replaceAll if you follow links
that far, and Java 1.5 introduces a method called
Matcher.quoteReplacement that can make this clearer than doing the
quoting by hand:

s.replaceAll(Pattern.quote("'"),
Matcher.quoteReplacement("\\'"));

This is a classic example of poor API design, and it's confused
thousands of people and cost thousands of man-hours of effort.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Knute Johnson

Chris said:
The subtlety worth pointing out here is that even the second parameter
to String.replaceAll is NOT a plain String. The API docs for
String.replaceAll are far from clear in pointing this out. It is
pointed out in the API docs for Matcher.replaceAll if you follow links
that far, and Java 1.5 introduces a method called
Matcher.quoteReplacement that can make this clearer than doing the
quoting by hand:

s.replaceAll(Pattern.quote("'"),
Matcher.quoteReplacement("\\'"));

This is a classic example of poor API design, and it's confused
thousands of people and cost thousands of man-hours of effort.

It sure is and it sure has!
 
R

Roedy Green

It sure is and it sure has!

--

if we had to do this over, i think you would have named methods:

replaceFirst, replaceAll, replacePatterns

or perhaps

replaceChars, replaceString, replaceStrings, replacePatterns
 
C

Chris

Roedy Green said:
if we had to do this over, i think you would have named methods:

replaceFirst, replaceAll, replacePatterns

or perhaps

replaceChars, replaceString, replaceStrings, replacePatterns

I think I would have done replaceAll(String, String) and also a
replaceAll(Pattern, String).

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top