Oliver Wong <> wrote:
> > I'm trying String.replaceAll("'","\\'")
> >
>
> 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