Question about java.util.regex.Pattern

Discussion in 'Java' started by frosted74@gmail.com, Mar 26, 2008.

  1. Guest

    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.
     
    , Mar 26, 2008
    #1
    1. Advertising

  2. 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 );
     
    Jussi Piitulainen, Mar 26, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 26, 10:16 am, Jussi Piitulainen <>
    wrote:
    > 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!!
     
    , Mar 26, 2008
    #3
  4. Roedy Green Guest

    On Wed, 26 Mar 2008 07:09:07 -0700 (PDT), 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
     
    Roedy Green, Mar 26, 2008
    #4
  5. Roedy Green Guest

    On Wed, 26 Mar 2008 16:56:55 GMT, Roedy Green
    <> 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
     
    Roedy Green, Mar 26, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Marquis

    util.regex.Pattern anomaly

    Roger Marquis, Aug 1, 2003, in forum: Java
    Replies:
    4
    Views:
    790
    Roger Marquis
    Aug 1, 2003
  2. Calum MacLean

    java.util.regex question

    Calum MacLean, Nov 28, 2003, in forum: Java
    Replies:
    3
    Views:
    433
    John C. Bollinger
    Dec 1, 2003
  3. joes
    Replies:
    2
    Views:
    1,046
    Daniel Pitts
    May 25, 2007
  4. Ichiro
    Replies:
    8
    Views:
    694
    Roedy Green
    Jul 13, 2009
  5. ChrisC
    Replies:
    4
    Views:
    186
    ChrisC
    Jun 25, 2010
Loading...

Share This Page