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

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

  3. techName = techName.replaceAll("[']", replacement);
    System.out.println( techName );
    ....
     
    Andrew Thompson, Dec 22, 2004
    #3
  4. Tim

    Esmond Pitt Guest

    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
     
    Josh Martin, Dec 29, 2004
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.