Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String.replaceAll(String regex, String replacement) question

Reply
Thread Tools

String.replaceAll(String regex, String replacement) question

 
 
Mladen Adamovic
Guest
Posts: n/a
 
      12-03-2003
I tried to replace " with \".
s.replaceAll("[\"]",\\\"");
but it does nothing!!

I tried then
s.replaceAll("[\Q"\E]","\Q\"\E")
but I got five sintax error?!?!?!?

I'm using JDK 1.4.2_01.

What's wrong?



 
Reply With Quote
 
 
 
 
Alan Moore
Guest
Posts: n/a
 
      12-04-2003
On Thu, 4 Dec 2003 17:40:10 +0100, "Mladen Adamovic" <(E-Mail Removed)>
wrote:

>I tried to replace all " with \".
>s=s.replaceAll("[\"]","\\\"");
>but it does nothing!! String remains the same!!!
>
>I tried then
>s=s.replaceAll("[\Q"\E]","\Q\"\E")
>but I got five sintax error?!?!?!?
>
> I'm using JDK 1.4.2_01.
>
>What's wrong?
>
>P.S. It was a typing error in first message I posted about this.
>


Actually, your first version does do something: it replaces every
double-quote with a double-quote. The first argument is okay
(although you don't need the square brackets), but the second argument
is short a couple of backslashes. See, first the Java compiler
processes the arguments, turning them into ["] and \". Then the
Matcher looks at that single backslash in the second argument, decides
it's only there to escape the double-quote, and throws it away. So
you need four backslashes to get one into the output, and one more to
escape the double-quote:

s = s.replaceAll("\"", "\\\\\"");

The \Q...\E construct won't help here, because both " and \ are
special to String literals, so they have to be escaped anyway. And
putting it inside a character class (square brackets), is at least
redundant, if not an error. And, most importantly, you only used one
backslash. If you want the Pattern compiler to "see" a single
backslash, you have to put two in the regex string. Except for
escaping double-quotes, backslashes should *always* appear in pairs in
a regex string, and you have to use four of them to actually match a
backslash.

Oh, and \Q...\E is totally meaningless in the replacement string.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-04-2003
On Wed, 3 Dec 2003 17:36:00 +0100, "Mladen Adamovic" <(E-Mail Removed)>
wrote or quoted :

>s.replaceAll("[\"]",\\\"");
>but it does nothing!!


You do realize this has no effect on s, just creates a new string
result.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Mladen Adamovic
Guest
Posts: n/a
 
      12-05-2003
> See, first the Java compiler
> processes the arguments, turning them into ["] and \". Then the
> Matcher looks at that single backslash in the second argument, decides
> it's only there to escape the double-quote, and throws it away.


Thanks for help Alan.
I really think that escape sequences shouldn't be processed TWICE when
program call
String.replaceAll(String regex, String replacement)





 
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
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
command string good in subprocess.Popen(string) fails inprocess.Process(string) Earl Eiland Python 3 05-16-2005 04:48 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments