java.util.regex: Backreferences?

Discussion in 'Java' started by dhek bhun kho, Jul 9, 2003.

  1. Hi there,

    I stumbled on this when I wanted to do a
    java.lang.String.replaceAll(String, String) call. The second argument just
    says 'replacement' in the API docs.

    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)

    The API doc is incomplete too! It's missing the description for the second
    argument, but whatever the case I can't do something like this:

    "hahahahahhaha".replaceAll("(a)(h)","\2\1");

    E.g. I want to produce something I'd normally get like this:

    $ echo "hahahaha" | sed "s/\(h\)\(a\)/\2\1/g"

    The only way to solve it would imply some relatively lengthy coding with
    the Pattern and Matcher classes.

    I was wondering if I am overlooking something. Is there no facility to
    easily perform regex substitutions using back references? If anybody can
    be sure the answer is no: please post.


    Greets
    Bhun.
    dhek bhun kho, Jul 9, 2003
    #1
    1. Advertising

  2. dhek bhun kho () wrote:
    : "hahahahahhaha".replaceAll("(a)(h)","\2\1");
    : E.g. I want to produce something I'd normally get like this:
    : $ echo "hahahaha" | sed "s/\(h\)\(a\)/\2\1/g"

    String s = "hahahahahhaha".replaceAll("(a)(h)","$2$1");
    System.out.println ("s: " + s);

    => s: hhahahahahhaa

    If you read the documentation it say to check the replaceAll method
    in Matcher....

    /robo
    Robert Olofsson, Jul 9, 2003
    #2
    1. Advertising

  3. (Robert Olofsson), Wed, 09 Jul 2003 10:45:53
    +0000:

    > dhek bhun kho () wrote:
    > : "hahahahahhaha".replaceAll("(a)(h)","\2\1");
    > : E.g. I want to produce something I'd normally get like this:
    > : $ echo "hahahaha" | sed "s/\(h\)\(a\)/\2\1/g"
    >
    > String s = "hahahahahhaha".replaceAll("(a)(h)","$2$1");
    > System.out.println ("s: " + s);
    >
    > => s: hhahahahahhaa
    >
    > If you read the documentation it say to check the replaceAll method
    > in Matcher....


    Thanks. I overlooked it. :( I don't understand why didn't put in the class
    documentation of Pattern.

    > /robo
    dhek bhun kho, Jul 9, 2003
    #3
    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. Roedy Green

    Re: java.util.regex.Matcher

    Roedy Green, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    365
    Roedy Green
    Jul 30, 2003
  2. Calum MacLean

    java.util.regex question

    Calum MacLean, Nov 28, 2003, in forum: Java
    Replies:
    3
    Views:
    413
    John C. Bollinger
    Dec 1, 2003
  3. Alex Polite
    Replies:
    14
    Views:
    951
  4. joes
    Replies:
    2
    Views:
    996
    Daniel Pitts
    May 25, 2007
  5. Wolfgang Thomas
    Replies:
    16
    Views:
    203
    Xicheng
    Mar 9, 2006
Loading...

Share This Page