String.replaceAll troubles with regEx

Discussion in 'Java' started by sugapablo@hotmail.com, Apr 17, 2006.

  1. Guest

    I'm trying to replace instances of a single quote "'" with a backslash
    single quote "\'".

    But I can't get it to work with the replaceAll function.
    http://java.sun.com/j2se/1.4.2/docs...replaceAll(java.lang.String, java.lang.String)

    I'm trying String.replaceAll("'","\\'")

    What am I doing wrong? Even when I put a "C" in the first argument, it
    only replaces it a single quote. Not a backslash and then a single
    quote.
    , Apr 17, 2006
    #1
    1. Advertising

  2. wrote:
    > I'm trying to replace instances of a single quote "'" with a backslash
    > single quote "\'".
    >
    > But I can't get it to work with the replaceAll function.
    > http://java.sun.com/j2se/1.4.2/docs...replaceAll(java.lang.String, java.lang.String)
    >
    > I'm trying String.replaceAll("'","\\'")
    >
    > What am I doing wrong? Even when I put a "C" in the first argument, it
    > only replaces it a single quote. Not a backslash and then a single
    > quote.
    >


    replaceAll("'","\\\\'");

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 17, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > I'm trying to replace instances of a single quote "'" with a backslash
    > single quote "\'".
    >
    > But I can't get it to work with the replaceAll function.
    > http://java.sun.com/j2se/1.4.2/docs...replaceAll(java.lang.String, java.lang.String)
    >
    > I'm trying String.replaceAll("'","\\'")
    >
    > What am I doing wrong? Even when I put a "C" in the first argument, it
    > only replaces it a single quote. Not a backslash and then a single
    > quote.
    >


    You need something like replaceAll("'","\\\\'"), with 4 blackslashes.
    There's two layers of escaping going on: One in Java, and one in the RegExp
    engine. See
    http://groups.google.com/group/comp.lang.java.programmer/msg/3fd11f7fb586e837
    for an explanation. When you write "\\\\'" in Java, the in-memory string
    becomes "\\'", and then the RegExp engine treats that as "\'".


    - Oliver
    Oliver Wong, Apr 17, 2006
    #3
  4. Chris Smith Guest

    Oliver Wong <> wrote:
    > > I'm trying String.replaceAll("'","\\'")
    > >

    >
    > You need something like replaceAll("'","\\\\'"), with 4 blackslashes.
    > There's two layers of escaping going on: One in Java, and one in the RegExp
    > engine.


    The subtlety worth pointing out here is that even the second parameter
    to String.replaceAll is NOT a plain String. The API docs for
    String.replaceAll are far from clear in pointing this out. It is
    pointed out in the API docs for Matcher.replaceAll if you follow links
    that far, and Java 1.5 introduces a method called
    Matcher.quoteReplacement that can make this clearer than doing the
    quoting by hand:

    s.replaceAll(Pattern.quote("'"),
    Matcher.quoteReplacement("\\'"));

    This is a classic example of poor API design, and it's confused
    thousands of people and cost thousands of man-hours of effort.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Apr 17, 2006
    #4
  5. Chris Smith wrote:
    > Oliver Wong <> wrote:
    >>> I'm trying String.replaceAll("'","\\'")
    >>>

    >> You need something like replaceAll("'","\\\\'"), with 4 blackslashes.
    >> There's two layers of escaping going on: One in Java, and one in the RegExp
    >> engine.

    >
    > The subtlety worth pointing out here is that even the second parameter
    > to String.replaceAll is NOT a plain String. The API docs for
    > String.replaceAll are far from clear in pointing this out. It is
    > pointed out in the API docs for Matcher.replaceAll if you follow links
    > that far, and Java 1.5 introduces a method called
    > Matcher.quoteReplacement that can make this clearer than doing the
    > quoting by hand:
    >
    > s.replaceAll(Pattern.quote("'"),
    > Matcher.quoteReplacement("\\'"));
    >
    > This is a classic example of poor API design, and it's confused
    > thousands of people and cost thousands of man-hours of effort.
    >


    It sure is and it sure has!

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 17, 2006
    #5
  6. Roedy Green Guest

    On 17 Apr 2006 07:38:52 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >I'm trying to replace instances of a single quote "'" with a backslash
    >single quote "\'".


    you are likely having problem with either regex or Java quoting.

    see http://mindprod.com/jgloss/regex.html#QUOTING
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 17, 2006
    #6
  7. Roedy Green Guest

    On Mon, 17 Apr 2006 11:45:32 -0700, Knute Johnson
    <> wrote, quoted or indirectly quoted someone
    who said :

    >> s.replaceAll(Pattern.quote("'"),
    >> Matcher.quoteReplacement("\\'"));
    >>
    >> This is a classic example of poor API design, and it's confused
    >> thousands of people and cost thousands of man-hours of effort.
    >>

    >
    >It sure is and it sure has!
    >
    >--


    if we had to do this over, i think you would have named methods:

    replaceFirst, replaceAll, replacePatterns

    or perhaps

    replaceChars, replaceString, replaceStrings, replacePatterns

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 17, 2006
    #7
  8. Chris Guest

    Roedy Green <> wrote:
    > if we had to do this over, i think you would have named methods:
    >
    > replaceFirst, replaceAll, replacePatterns
    >
    > or perhaps
    >
    > replaceChars, replaceString, replaceStrings, replacePatterns


    I think I would have done replaceAll(String, String) and also a
    replaceAll(Pattern, String).

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris, Apr 19, 2006
    #8
    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. sfu
    Replies:
    6
    Views:
    18,237
  2. Tony
    Replies:
    2
    Views:
    557
    Alan Moore
    Dec 4, 2003
  3. Mladen Adamovic
    Replies:
    0
    Views:
    723
    Mladen Adamovic
    Dec 4, 2003
  4. Mladen Adamovic
    Replies:
    3
    Views:
    14,560
    Mladen Adamovic
    Dec 5, 2003
  5. Replies:
    6
    Views:
    49,246
    Bernd Klier
    May 29, 2006
Loading...

Share This Page