Robert Klemme
8/30/2006 11:29:00 AM
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