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 / March 2008

Tip: Looking for answers? Try searching our database.

Question about java.util.regex.Pattern

Thread view: 
frosted74@gmail.com - 26 Mar 2008 15:09 GMT
Hi,

I am trying to use a Pattern to match certain strings in error
messages in our application.  We are delimiting them using the
following notation, {0}, {1}, etc.  I am trying to use String's
replaceFirst (even replaceAll) method to replace the instances of
these tokens with appropriate Strings.  I am using the following code:

String match = "\\{0\\}";
String message = "{0} should be 5 characters in length.";
String replacement = "Zip Code";
message.replaceAll( match, replacement );

message is unaffected by this method call.

This pattern /\{0\}/ works on online RegEx testers, but I feel I am
missing an important step porting it to java's Pattern class.  I have
tried sticking /'s into the string, ending it with "g", etc.  Nothing
seems to work.  anyone have any insight into how I can do this or what
I am doing incorrectly?

TIA.
Jussi Piitulainen - 26 Mar 2008 15:16 GMT
> String message = "{0} should be 5 characters in length.";
> String replacement = "Zip Code";
> message.replaceAll( match, replacement );
>
> message is unaffected by this method call.

Strings are immutable. The method returns a new String.
message = message.replaceAll( match, replacement );
frosted74@gmail.com - 26 Mar 2008 15:26 GMT
On Mar 26, 10:16 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> froste...@gmail.com writes:
> > String message = "{0} should be 5 characters in length.";
[quoted text clipped - 5 lines]
> Strings are immutable. The method returns a new String.
> message = message.replaceAll( match, replacement );

*Slaps self in forehead*.  Thanks Jussi!!
Roedy Green - 26 Mar 2008 17:56 GMT
>I am trying to use a Pattern to match certain strings in error
>messages in our application.  We are delimiting them using the
>following notation, {0}, {1}, etc.  I am trying to use String's
>replaceFirst (even replaceAll) method to replace the instances of
>these tokens with appropriate Strings.  I am using the following code:

you don't want a regex replace.  You are just looking for an ordinary
string. "{0}"

so use String.replace to replace all, not String.replaceAll.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 26 Mar 2008 20:55 GMT
On Wed, 26 Mar 2008 16:56:55 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>so use String.replace to replace all, not String.replaceAll.

see http://mindprod.com/jgloss/string.html
for basic hints on using the class, including gotchas.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.