[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular Expressions

Peter Meier

8/30/2006 10:30:00 AM

hi,

is there any way to reuse a group in a regular expression in ruby?

In other languages you can reuse a group by write (?<name> ....).

But I can't figure out how to do this in ruby?

Do you have some advice?

Thanks
Peter
2 Answers

Robert Klemme

8/30/2006 11:29:00 AM

0

On 30.08.2006 12:29, Peter Meier wrote:
> hi,
>
> is there any way to reuse a group in a regular expression in ruby?
>
> In other languages you can reuse a group by write (?<name> ....).
>
> But I can't figure out how to do this in ruby?
>
> Do you have some advice?

Ruby's current regexp engine does not support named groups. You can
however reference by count:

>> /(b+)a+\1/ =~ ("aaabbb"*10)
=> 3
>> ("aaabbb"*10).scan /(b+)a+\1/
=> [["bbb"], ["bbb"], ["bbb"], ["bbb"], ["bbb"]]
>> ("aaabbb"*10).scan /((b+)a+\2)/
=> [["bbbaaabbb", "bbb"], ["bbbaaabbb", "bbb"], ["bbbaaabbb", "bbb"],
["bbbaaabbb", "bbb"], ["bbbaaabbb", "bbb"]]
>>

HTH

robert

David Vallner

9/1/2006 7:03:00 PM

0

Robert Klemme wrote:
> Ruby's current regexp engine does not support named groups.

For the sake of providing complete information, Ruby's next regexp
engine (oniguruma) will. You can play around with it in the 1.9 branch
(at least it was used there last time I checked.) I don't know if you
can use it standalone.

David Vallner