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 / February 2006

Tip: Looking for answers? Try searching our database.

String replaceAll doesn't like commas

Thread view: 
O.B. - 31 Jan 2006 02:27 GMT
Why doesn't the following turn "Bubba, Inc." into "Bubba\, Inc."?

String test = "Bubba, Inc.";
test = test.replaceAll(",", "\\,");
Stefan Ram - 31 Jan 2006 02:34 GMT
"O.B." <funkjunk@bellsouth.net> did not write:
>Why doesn't the following turn "Alpha, Inc." into "Alpha\, Inc."?
>String test = "Alpha, Inc.";
>test = test.replaceAll(",", "\\,");

 Why not just read about it in the documentation?

http://download.java.net/jdk6/docs/api/java/lang/String.html#replaceAll(java.lan
g.String,%20java.lang.String
)

O.B. - 31 Jan 2006 02:53 GMT
> "O.B." <funkjunk@bellsouth.net> did not write:
>
[quoted text clipped - 5 lines]
>
> http://download.java.net/jdk6/docs/api/java/lang/String.html#replaceAll(java.lan
g.String,%20java.lang.String
)

What's wrong with my regular expression?
mikm - 31 Jan 2006 03:03 GMT
Change "\\," to "\\\\,"
Roedy Green - 31 Jan 2006 04:39 GMT
>>>test = test.replaceAll(",", "\\,");
>>
[quoted text clipped - 3 lines]
>
>What's wrong with my regular expression?

\\ is a magic character to regexes and in a magic character to Java
string literals.  You have to "quote" it for both. The docs are not
clear unless you read up on Matcher.replaceAll, but he second parm is
a regex too, not just a simple string.

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

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

Roedy Green - 31 Jan 2006 04:53 GMT
On Tue, 31 Jan 2006 04:39:29 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>>What's wrong with my regular expression?
>
>\\ is a magic character to regexes and in a magic character to Java
>string literals.  You have to "quote" it for both. The docs are not
>clear unless you read up on Matcher.replaceAll, but he second parm is
>a regex too, not just a simple string.

see http://mindprod.com/jgloss/string.html#REPLACE
Signature

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

George - 31 Jan 2006 02:58 GMT
> Why doesn't the following turn "Bubba, Inc." into "Bubba\, Inc."?
>
> String test = "Bubba, Inc.";
> test = test.replaceAll(",", "\\,");

Try this:

test = test.replaceAll(",", "\\\\,");
Jeffrey Schwab - 31 Jan 2006 03:14 GMT
> Why doesn't the following turn "Bubba, Inc." into "Bubba\, Inc."?
>
> String test = "Bubba, Inc.";
> test = test.replaceAll(",", "\\,");

The double quotes eat one backslash.  The regex parser eats the other.
Try "\\\\,".
Alan Krueger - 31 Jan 2006 21:38 GMT
>> Why doesn't the following turn "Bubba, Inc." into "Bubba\, Inc."?
>>
[quoted text clipped - 3 lines]
> The double quotes eat one backslash.  The regex parser eats the other.
> Try "\\\\,".

It gets really fun when you want to quote backslashes in a string with
an additional backslash:

someString.replaceAll("\\\\", "\\\\\\\\");

That's a lot of backslashes.
Stefan Ram - 31 Jan 2006 21:49 GMT
>someString.replaceAll("\\\\", "\\\\\\\\");
>That's a lot of backslashes.

class String
{ final java.lang.String string;
 public String( final java.lang.String string )
 { this.string = string; }
 public java.lang.String replaceAll
 ( final java.lang.String search,
   final java.lang.String replace )
 { return this.string.replaceAll
   ( java.util.regex.Pattern.quote( search ),
     java.util.regex.Matcher.quoteReplacement( replace )); }}
public class Main
{ public static void main( final java.lang.String[] args )
 { java.lang.System.out.println
   ( new String( "a\\b" ).replaceAll( "\\", "\\\\" )); }}
 
Alan Krueger - 01 Feb 2006 05:05 GMT
>   { return this.string.replaceAll
>     ( java.util.regex.Pattern.quote( search ),
>       java.util.regex.Matcher.quoteReplacement( replace )); }}

Some of us don't have the pleasure of being able to target a 1.5 JRE/JDK
yet.
Roedy Green - 31 Jan 2006 04:25 GMT
>String test = "Bubba, Inc.";
>test = test.replaceAll(",", "\\,");
see
http://mindprod.com/jgloss/string.html#REPLACE
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



©2009 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.