java.util.regex: Backreferences?

D

dhek bhun kho

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

Robert Olofsson

dhek bhun kho ([email protected]) 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
 
D

dhek bhun kho

(e-mail address removed) (Robert Olofsson), Wed, 09 Jul 2003 10:45:53
+0000:
dhek bhun kho ([email protected]) 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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top