Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

String.replaceAll troubles with regEx

Thread view: 
sugapablo@hotmail.com - 17 Apr 2006 15:38 GMT
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/api/java/lang/String.html#replaceAll(java.la
ng.String,%20java.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.
Knute Johnson - 17 Apr 2006 16:25 GMT
> I'm trying to replace instances of a single quote "'" with a backslash
> single quote "\'".
[quoted text clipped - 7 lines]
> only replaces it a single quote.  Not a backslash and then a single
> quote.

replaceAll("'","\\\\'");

Signature

Knute Johnson
email s/nospam/knute/

Oliver Wong - 17 Apr 2006 16:41 GMT
> I'm trying to replace instances of a single quote "'" with a backslash
> single quote "\'".
[quoted text clipped - 7 lines]
> 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
Chris Smith - 17 Apr 2006 16:51 GMT
> > 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Knute Johnson - 17 Apr 2006 19:45 GMT
>>> I'm trying String.replaceAll("'","\\'")
>>>
[quoted text clipped - 15 lines]
> 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!

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 17 Apr 2006 21:26 GMT
On Mon, 17 Apr 2006 11:45:32 -0700, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>>     s.replaceAll(Pattern.quote("'"),
>>                  Matcher.quoteReplacement("\\'"));
[quoted text clipped - 3 lines]
>
>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

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris - 19 Apr 2006 17:50 GMT
> if we had to do this over, i think you would have named methods:
>
[quoted text clipped - 3 lines]
>
> replaceChars, replaceString, replaceStrings, replacePatterns

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

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 17 Apr 2006 20:10 GMT
>I'm trying to replace instances of a single quote "'" with a backslash
>single quote "\'".

you are likely having problem with either regex or Java quoting.

see http://mindprod.com/jgloss/regex.html#QUOTING
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.