String replaceAll fails

Discussion in 'Java' started by Tim, Dec 22, 2004.

  1. Tim

    Tim Guest

    // what's wrong with this code?

    public class TestReplaceAll {


    public static void main(String[] args) {

    String techName = "O'brien, Jones";
    String replacement = "\\'";
    System.out.println( techName.replaceAll("[']", replacement) );
    System.out.println( techName.replaceAll("[']", "\'" ) );
    System.out.println( techName.replaceAll("[']", "\\'" ) );
    System.out.println( techName.replaceAll("[']", "\\''" ) );
    }
    }

    // here's the output
    O'brien, Jones
    O'brien, Jones
    O'brien, Jones
    O''brien, Jones

    // And here's the documentation on the replaceAll function:
    replaceAll
    public String replaceAll(String regex,
    String replacement)Replaces each substring of
    this string that matches the given regular expression with the given
    replacement.
    An invocation of this method of the form str.replaceAll(regex, repl)
    yields exactly the same result as the expression
    El bugaroo!
    TimJowers
     
    Tim, Dec 22, 2004
    #1
    1. Advertising

  2. Tim

    Josh Martin Guest

    > // what's wrong with this code?

    Looks like it's working fine to me - what did you expect as output?
     
    Josh Martin, Dec 22, 2004
    #2
    1. Advertising

  3. On 21 Dec 2004 17:17:42 -0800, Tim wrote:

    > // what's wrong with this code?
    >
    > public class TestReplaceAll {
    >
    >
    > public static void main(String[] args) {
    >
    > String techName = "O'brien, Jones";
    > String replacement = "\\'";


    techName = techName.replaceAll("[']", replacement);
    System.out.println( techName );
    ....

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 22, 2004
    #3
  4. Tim

    Esmond Pitt Guest

    Andrew Thompson wrote:
    > techName = techName.replaceAll("[']", replacement);
    > System.out.println( techName );


    That's formally equivalent to what he did.
     
    Esmond Pitt, Dec 22, 2004
    #4
  5. Tim

    Tim Guest

    The bug was mine/documentation.

    public String replaceAll(String regex,String replacement)
    Replaces each substring of this string that matches the
    given regular expression with the given replacement.

    I assumed they meant "replacement [String]" but they meant
    "replacement [special set of characters]". Where the special set of
    characters is at least partially defined in the doc for the Matcher
    class and means you'll get unexpected behavior if the replacement
    String contains \ or $ and you did not realize the behavior is that of
    Matcher.replaceAll.

    The correct solution for me was
    techName = techName.replaceAll("[']", "\\\\'");
    as I needed to change each ' to a \' for use as parameters to a
    javascript function.

    Maybe someone has written a RegEx class that uses methods and
    attributes rather than special characters to carry out its functions?
    Thanks for the feedback,
    TimJowers
     
    Tim, Dec 22, 2004
    #5
  6. Tim

    Josh Martin Guest

    Tim -

    Welcome to the wonderful world of regular expressions in Java :)

    Escaping characters is a bit unwieldy - I always forget the part of setting
    the original String (i.e. techName = techName.replaceAll...).

    Josh

    "Tim" <> wrote in message
    news:...
    >
    > The bug was mine/documentation.
    >
    > public String replaceAll(String regex,String replacement)
    > Replaces each substring of this string that matches the
    > given regular expression with the given replacement.
    >
    > I assumed they meant "replacement [String]" but they meant
    > "replacement [special set of characters]". Where the special set of
    > characters is at least partially defined in the doc for the Matcher
    > class and means you'll get unexpected behavior if the replacement
    > String contains \ or $ and you did not realize the behavior is that of
    > Matcher.replaceAll.
    >
    > The correct solution for me was
    > techName = techName.replaceAll("[']", "\\\\'");
    > as I needed to change each ' to a \' for use as parameters to a
    > javascript function.
    >
    > Maybe someone has written a RegEx class that uses methods and
    > attributes rather than special characters to carry out its functions?
    > Thanks for the feedback,
    > TimJowers
    >
     
    Josh Martin, Dec 29, 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. sfu
    Replies:
    6
    Views:
    18,783
  2. Mladen Adamovic
    Replies:
    0
    Views:
    777
    Mladen Adamovic
    Dec 4, 2003
  3. Mladen Adamovic
    Replies:
    3
    Views:
    14,782
    Mladen Adamovic
    Dec 5, 2003
  4. Peebster_99
    Replies:
    5
    Views:
    19,033
    Mladen Adamovic
    Mar 7, 2004
  5. William Krick
    Replies:
    4
    Views:
    78,029
    rt19983
    Dec 18, 2008
Loading...

Share This Page