From: "David A. Black" Date: 2009-06-21T21:55:11+09:00 Subject: Re: regular expression gurus--help! --1926193751-996145823-1245588898=:8020 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-996145823-1245588898=:8020" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-996145823-1245588898=:8020 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Sun, 21 Jun 2009, Hooopo wrote: > On Jun 21, 4:08�pm, Harry Kakueki wrote: >> On Sun, Jun 21, 2009 at 4:10 PM, timr wrote: >>> I want all possible three letter sets for a string. Scan does this: >>> irb> >> >>> But I need: >>> # >> abc >>> # >> bcd >>> # >> cde >>> # >> def >>> etc. >> >>> I used to know the secret spell, but forgot it. Does anyone know how >>> to do this with regular expression >> >> Here is one way to get that result. >> (But not with a regular expression) >> Not all 3 letter sets but I think this is the result you are really looking for. >> >> str = "abcdefghijkl" >> puts str.unpack("a3X2"*(str.length-2)) >> >> #output >> abc >> bcd >> cde >> def >> efg >> fgh >> ghi >> hij >> ijk >> jkl >> >> Harry >> >> -- >> A Look into Japanese Ruby List in Englishhttp://www.kakueki.com/ruby/list.html > > require'enumerator' > "qwertyuiopasdfgd".scan(/./).each_cons(3){|x,y,z| p [x,y,z].join} Here's another variant on this one, for 1.9: >> str = "abcdefghij" => "abcdefghij" >> str.chars.each_cons(3).map {|c| c.join } => ["abc", "bcd", "cde", "def", "efg", "fgh", "ghi", "hij"] David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com --1926193751-996145823-1245588898=:8020-- --1926193751-996145823-1245588898=:8020--