Regexp reference

Please help me with the following problem:

/ ( ( a ) ( b ) ) ©/
1 2 3 4

in Match Data, it seems that $1… $4 are defined as above. How can I let
regex.match(string) method to return an array like this:

[ [a,b], c] ?

Thanks!
Shannon

String#scan does it.

···

----- Original Message -----
From: “Shannon Fang” xrfang@hotmail.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, December 06, 2002 1:26 PM
Subject: regexp reference

Please help me with the following problem:

/ ( ( a ) ( b ) ) (c)/
1 2 3 4

in Match Data, it seems that $1… $4 are defined as above. How can I let
regex.match(string) method to return an array like this:

[ [a,b], c] ?

Thanks!
Shannon

Perhaps I’m being stupid here, but what’s wrong about

irb(main):003:0> “abc” =~ /(?:(a)(b))(c)/
0
irb(main):004:0> p [[$1,$2],$3]
[[“a”, “b”], “c”]
nil

?

···

On Sat, Dec 07, 2002 at 06:26:39AM +0900, Shannon Fang wrote:

Please help me with the following problem:

/ ( ( a ) ( b ) ) (c)/
1 2 3 4

in Match Data, it seems that $1… $4 are defined as above. How can I let
regex.match(string) method to return an array like this:

[ [a,b], c] ?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Ok, I’m just uploading the new version of the kernel, v1.3.33, also
known as “the buggiest kernel ever”.
– Linus Torvalds

String.scan is better for my purpose. because I need to group the
sub-matches by using join. Thanks anyway.

Shannon

···

On Sat, 7 Dec 2002 07:18:29 +0900 Mauricio Fern~{an~}dez batsman.geo@yahoo.com wrote:

On Sat, Dec 07, 2002 at 06:26:39AM +0900, Shannon Fang wrote:

Please help me with the following problem:

/ ( ( a ) ( b ) ) (c)/
1 2 3 4

in Match Data, it seems that $1… $4 are defined as above. How can I let
regex.match(string) method to return an array like this:

[ [a,b], c] ?

Perhaps I’m being stupid here, but what’s wrong about

irb(main):003:0> “abc” =~~ /(?:(a)(b))(c)/
0
irb(main):004:0> p [[$1,$2],$3]
[[“a”, “b”], “c”]
nil

?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Ok, I’m just uploading the new version of the kernel, v1.3.33, also
known as “the buggiest kernel ever”.
– Linus Torvalds