From: Charles Mills Date: 2005-02-13T14:09:58+09:00 Subject: Re: duplicated match question shaobo.li@gmail.com wrote: > Hi, > > I am new to Ruby,and confusing about following regular patterns: > > why pattern "(\w)\1" match duplicated letter "ll" in "Hello" (\w) matches a word character and stores it in the first 'register'. \1 is the literal value of the first register. So, /(\w)\1/ matches a word character followed by that same word character. irb(main):004:0> /(\w)\1/ =~ 'll' => 0 irb(main):005:0> /(\w)\1/ =~ 'aa' => 0 irb(main):006:0> /(\w)\1/ =~ 'bb' => 0 irb(main):007:0> /(\w)\1/ =~ '11' => 0 irb(main):008:0> /(\w)\1/ =~ '##' => nil '#' is not a word character. > and "(\w+)\1" match "ississ" in "Mississippi"? /(\w+)\1/ matches a sequence of word characters followed by that same sequence of word characters. In 'Mississippi' the (\w+) matches 'iss' and the \1 which resolves to 'iss' matches 'iss' a second time, giving a complete match of /(\w+)\1/. Also note: irb(main):009:0> /(\w+)\1/ =~ 'ssissi' => 0 irb(main):010:0> $1 => "ssi" irb(main):011:0> $& => "ssissi" Hope that helps, Charlie