I have the following case that does not work the way I expect, and, I am doing something wrong, but I cannot find out what that is. The script matches the four letter words within a sentence. I want to find a way to iterate over the matched groups.
x = "This is a statement with four letter words like this"
result = x.match /(\b\w{4}\b)/
=> #<MatchData "This" 1:"This">
Unfortunately, $1
contains "This"
, but this is all I get. $2
should contain "with"
but it is nil
. What am I doing wrong? Why is $2
nil
? Why is $n
with n>=2
nil?