2

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?

2
  • Perl, and other languages, support the g flag which tells their Regular Expression parser to look through the target string for all matches. Ruby doesn't have g, so we use scan as @lucapette recommends. Commented Jan 27, 2012 at 17:22
  • 1
    String#match is usually used in boolean context. Commented Jan 27, 2012 at 19:18

1 Answer 1

9

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"]]
Sign up to request clarification or add additional context in comments.

2 Comments

Why not add some example output so the OP sees why it is the right answer?
Thanks, I misunderstood String#match I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.