String.replaceAll problem processing "value { { {"

Discussion in 'Java' started by Peebster_99, Mar 5, 2004.

  1. Peebster_99

    Peebster_99 Guest

    I would like to use String.replaceAll() in order to replace substrings
    that equal: "value { { {" with this string: "value { {".

    When attempting to invoke replaceAll as follows:
    String s = ("value[\\s]{\\s");
    output.replaceAll(s ,"value { {");

    The following error result below produced at runtime. Using \\s for
    whitespace yields similar results. Can someone point out to me what
    I'm doing wrong here?
    Thanks,

    java.util.regex.PatternSyntaxException: Illegal repetition near index
    5
    value { { {
    ^
    java.util.regex.PatternSyntaxException: Illegal repetition near index
    5
    value { { {
    ^
    Peebster_99, Mar 5, 2004
    #1
    1. Advertising

  2. (Peebster_99) writes:

    > String s = ("value[\\s]{\\s");


    { is a special character in regular expressions, you need to escape
    that too.

    (It's used in POSIX character classes and quantifiers, e.g. (foo){1,2}
    will match foo and foofoo but not foofoofoo.)

    (Removed non-existing-on-proper-newsservers group.)
    Tor Iver Wilhelmsen, Mar 5, 2004
    #2
    1. Advertising

  3. Peebster_99

    hiwa Guest

    (Peebster_99) wrote in message news:<>...
    > I would like to use String.replaceAll() in order to replace substrings
    > that equal: "value { { {" with this string: "value { {".
    >
    > When attempting to invoke replaceAll as follows:
    > String s = ("value[\\s]{\\s");
    > output.replaceAll(s ,"value { {");
    >
    > The following error result below produced at runtime. Using \\s for
    > whitespace yields similar results. Can someone point out to me what
    > I'm doing wrong here?
    > Thanks,
    >
    > java.util.regex.PatternSyntaxException: Illegal repetition near index
    > 5
    > value { { {
    > ^
    > java.util.regex.PatternSyntaxException: Illegal repetition near index
    > 5
    > value { { {
    > ^


    '{' is a meta character for Java regexp engine. You have to escape
    them with two backslashes prepending.
    hiwa, Mar 6, 2004
    #3
  4. Don't forget also that String.replaceAll process its param
    String twice, so if you want to to replace "\\" wth something instead of
    replace "\\\\" you must write replace "\\\\\\\\\\". Don't ask me why thay
    make it that way.

    > '{' is a meta character for Java regexp engine. You have to escape
    > them with two backslashes prepending.
    Mladen Adamovic, Mar 6, 2004
    #4
  5. Peebster_99

    hiwa Guest

    "Mladen Adamovic" <> wrote in message news:<c2coib$p0f$>...
    > Don't forget also that String.replaceAll process its param
    > String twice, so if you want to to replace "\\" wth something instead of
    > replace "\\\\" you must write replace "\\\\\\\\\\". Don't ask me why thay
    > make it that way.
    >
    > > '{' is a meta character for Java regexp engine. You have to escape
    > > them with two backslashes prepending.


    > String.replaceAll process its param String twice

    Ugh! That was my heavy headache during a recent development. I'd like
    to know where did you get that info? Sun javadoc doesn't have no
    mention about it.

    //from my working code ... after a muddy struggle:
    preBody = preBody.replaceAll("\\\\", "\\\\\\\\");
    hiwa, Mar 7, 2004
    #5
  6. "hiwa" <> wrote:
    > > String.replaceAll process its param String twice

    > Ugh! That was my heavy headache during a recent development. I'd like
    > to know where did you get that info? Sun javadoc doesn't have no
    > mention about it.


    I have same headache and asked three months ago at the c.l.j.p and one man
    help me, unfortunatly I don't know his name at the moment, but THANKS TO
    HIM!!!

    And it was same replaceAll statement as you mention here:

    > //from my working code ... after a muddy struggle:
    > preBody = preBody.replaceAll("\\\\", "\\\\\\\\");
    Mladen Adamovic, Mar 7, 2004
    #6
    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. Mladen Adamovic
    Replies:
    0
    Views:
    726
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,565
    Mladen Adamovic
    Dec 5, 2003
  3. Muhammad Ali

    String replaceAll problem

    Muhammad Ali, Apr 13, 2007, in forum: Java
    Replies:
    3
    Views:
    566
    Tris Orendorff
    Apr 14, 2007
  4. Ed
    Replies:
    116
    Views:
    2,322
  5. Ed
    Replies:
    41
    Views:
    1,772
Loading...

Share This Page