如何为regex获取多个组?

时间:2022-09-28 16:10:54

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

检查:http://rubular.com/r/48K8o5mzUX

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

如何在Ruby中为regex获取多个组?

1 个解决方案

#1


7  

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"]]

#1


7  

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"]]