Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.util.regex: Backreferences?

Reply
Thread Tools

java.util.regex: Backreferences?

 
 
dhek bhun kho
Guest
Posts: n/a
 
      07-09-2003
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/...va.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.
 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      07-09-2003
dhek bhun kho ((E-Mail Removed)) 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
 
Reply With Quote
 
 
 
 
dhek bhun kho
Guest
Posts: n/a
 
      07-09-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Robert Olofsson), Wed, 09 Jul 2003 10:45:53
+0000:

> dhek bhun kho ((E-Mail Removed)) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments