Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question about java.util.regex.Pattern

Reply
Thread Tools

Question about java.util.regex.Pattern

 
 
frosted74@gmail.com
Guest
Posts: n/a
 
      03-26-2008
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.
 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      03-26-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> 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 );
 
Reply With Quote
 
 
 
 
frosted74@gmail.com
Guest
Posts: n/a
 
      03-26-2008
On Mar 26, 10:16 am, Jussi Piitulainen <(E-Mail Removed)>
wrote:
> (E-Mail Removed) writes:
> > 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 );


*Slaps self in forehead*. Thanks Jussi!!
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-26-2008
On Wed, 26 Mar 2008 07:09:07 -0700 (PDT), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

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

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-26-2008
On Wed, 26 Mar 2008 16:56:55 GMT, Roedy Green
<(E-Mail Removed)> 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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments