Regular Expressions

P

Peter Meier

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
 
R

Robert Klemme

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
 
D

David Vallner

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top