String replaceAll() and regex question

Discussion in 'Java' started by grasp06110@yahoo.com, May 28, 2006.

  1. Guest

    Hi All,

    I would like to hand the replaceAll() method of String text that it
    would interpret as literal text and not as a regex. In the code I am
    trying to implement I am given two variables and a string and need to
    replace one variable with the second in the string. If either of the
    variables contains a special character (e.g. $) the replaceAll() method
    can fail. An example of this is shown below. In the example below I
    can escape the $ character with /$ but I don't want to have to search
    each string for every potential trouble maker. I would like to tell
    the regex to treat the entire string as a literal.

    Any help would be appreciated.

    Thanks,
    John


    public class Ouch {

    public static void main(String[] args) throws Exception {
    String start = "yours now for just <amount>";
    String price = "$9.99";
    String end = start.replaceAll("<amount>", price);
    System.out.println(end);
    }

    }

    Exception in thread "main" java.lang.IndexOutOfBoundsException: No
    group 9
    at java.util.regex.Matcher.group(Matcher.java:463)
    at java.util.regex.Matcher.appendReplacement(Matcher.java:730)
    at java.util.regex.Matcher.replaceAll(Matcher.java:806)
    at java.lang.String.replaceAll(String.java:2000)
    at Ouch.main(Ouch.java:7)
    , May 28, 2006
    #1
    1. Advertising

  2. wrote in news:1148773195.171205.166250@
    38g2000cwa.googlegroups.com:

    > I would like to hand the replaceAll() method of String text that it
    > would interpret as literal text and not as a regex.


    What you probably want to use is actually String.replace(). The name is
    unfortunate - it replaces more than one occurence, even if it is easy to
    believe otherwise by just seeing the name (together with replaceAll()). see
    the API for more info.

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace
    (char,%20char)


    --
    Martin Gerner
    Martin Gerner, May 28, 2006
    #2
    1. Advertising

  3. Martin Gerner <> wrote in
    news:Xns97D119D348608martindotgerneratnos@129.16.222.141:

    > wrote in news:1148773195.171205.166250@
    > 38g2000cwa.googlegroups.com:
    >
    >> I would like to hand the replaceAll() method of String text that it
    >> would interpret as literal text and not as a regex.

    >
    > What you probably want to use is actually String.replace(). The name
    > is unfortunate - it replaces more than one occurence, even if it is
    > easy to believe otherwise by just seeing the name (together with
    > replaceAll()). see the API for more info.
    >
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace
    > (char,%20char)
    >
    >


    Hm, the link got bogged up. Well, just scroll down until you get on
    replace.

    --
    Martin Gerner
    Martin Gerner, May 28, 2006
    #3
  4. Chris Smith Guest

    <> wrote:
    > I would like to hand the replaceAll() method of String text that it
    > would interpret as literal text and not as a regex. In the code I am
    > trying to implement I am given two variables and a string and need to
    > replace one variable with the second in the string. If either of the
    > variables contains a special character (e.g. $) the replaceAll() method
    > can fail.


    Yep. See the static methods Pattern.quote and Matcher.quoteReplacement.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, May 28, 2006
    #4
  5. Guest

    Hi John!!
    I am not sure why this is not working. But have you tried this with
    StringBuffer?

    wrote:
    > Hi All,
    >
    > I would like to hand the replaceAll() method of String text that it
    > would interpret as literal text and not as a regex. In the code I am
    > trying to implement I am given two variables and a string and need to
    > replace one variable with the second in the string. If either of the
    > variables contains a special character (e.g. $) the replaceAll() method
    > can fail. An example of this is shown below. In the example below I
    > can escape the $ character with /$ but I don't want to have to search
    > each string for every potential trouble maker. I would like to tell
    > the regex to treat the entire string as a literal.
    >
    > Any help would be appreciated.
    >
    > Thanks,
    > John
    >
    >
    > public class Ouch {
    >
    > public static void main(String[] args) throws Exception {
    > String start = "yours now for just <amount>";
    > String price = "$9.99";
    > String end = start.replaceAll("<amount>", price);
    > System.out.println(end);
    > }
    >
    > }
    >
    > Exception in thread "main" java.lang.IndexOutOfBoundsException: No
    > group 9
    > at java.util.regex.Matcher.group(Matcher.java:463)
    > at java.util.regex.Matcher.appendReplacement(Matcher.java:730)
    > at java.util.regex.Matcher.replaceAll(Matcher.java:806)
    > at java.lang.String.replaceAll(String.java:2000)
    > at Ouch.main(Ouch.java:7)
    , May 28, 2006
    #5
  6. Guest

    Hi,

    Martin Gerner's suggestion of using String.replace looks like it should
    work. My bad for not looking at the api more closely.

    Thanks!
    John
    , May 28, 2006
    #6
  7. Bernd Klier Guest

    wrote:
    > Hi All,


    Hi

    >
    > public class Ouch {
    >
    > public static void main(String[] args) throws Exception {
    > String start = "yours now for just <amount>";
    > String price = "$9.99";
    > String end = start.replaceAll("<amount>", price);
    > System.out.println(end);
    > }
    >
    > }
    >
    > Exception in thread "main" java.lang.IndexOutOfBoundsException: No
    > group 9


    You have to escape the '$' character. Try
    String price = "\\$9.99";
    and it'll work for you.

    HTH

    Bernd
    Bernd Klier, May 29, 2006
    #7
    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,249
  2. Tony
    Replies:
    2
    Views:
    560
    Alan Moore
    Dec 4, 2003
  3. Mladen Adamovic
    Replies:
    0
    Views:
    725
    Mladen Adamovic
    Dec 4, 2003
  4. Mladen Adamovic
    Replies:
    3
    Views:
    14,565
    Mladen Adamovic
    Dec 5, 2003
  5. Replies:
    7
    Views:
    21,836
    Chris
    Apr 19, 2006
Loading...

Share This Page