Regular Expressions

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 (? …).

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

Do you have some advice?

Thanks
Peter

On 30.08.2006 12:29, Peter M. 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 (? …).

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

Robert K. 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 V.