Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String.replaceAll troubles with regEx

Reply
Thread Tools

String.replaceAll troubles with regEx

 
 
sugapablo@hotmail.com
Guest
Posts: n/a
 
      04-17-2006
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/...va.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.

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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/...va.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.
>


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

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      04-17-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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/...va.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....d11f7fb586e837
for an explanation. When you write "\\\\'" in Java, the in-memory string
becomes "\\'", and then the RegExp engine treats that as "\'".


- Oliver

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      04-17-2006
Oliver Wong <(E-Mail Removed)> 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
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      04-17-2006
Chris Smith wrote:
> Oliver Wong <(E-Mail Removed)> 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.
>


It sure is and it sure has!

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-17-2006
On 17 Apr 2006 07:38:52 -0700, (E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-17-2006
On Mon, 17 Apr 2006 11:45:32 -0700, Knute Johnson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>> 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!
>
>--


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

replaceFirst, replaceAll, replacePatterns

or perhaps

replaceChars, replaceString, replaceStrings, replacePatterns

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      04-19-2006
Roedy Green <(E-Mail Removed) > wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Encounter troubles with Regex in Chinese text splitting Mike Meng Ruby 3 12-03-2005 02:10 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments