Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get multiple groups for a regex?

Tags:

regex

ruby

This code:

string1 = "I will drill for a well in walla walla washington."
/(w.ll) /.match(string1)

is returning only will.

Shouldn't it be returning will and well also?

Check: http://rubular.com/r/48K8o5mzUX

How do I get multiple groups for a regex in Ruby?

like image 812
Federico Avatar asked Mar 01 '26 20:03

Federico


1 Answers

It's working fine and it's the expected behaviour. Probably you want to use scan, like in the following:

1.9.2 (main):0 > string1.scan(/(w.ll)/)
=> [["will"], ["well"], ["wall"], ["wall"]]
like image 113
lucapette Avatar answered Mar 04 '26 12:03

lucapette