From: "David A. Black" Date: 2005-08-25T00:57:47+09:00 Subject: Re: Novice Q: What's the difference between /\s*/ and /(\s)*/? Hi -- On Thu, 25 Aug 2005, Jeff Wood wrote: > Ok, now for a clean and simple answer... > > The parenthesis create "groups". Groups give you the ability to save > away parts of the matched pattern for easy access after a match has > been made. And yes, the O'Reilly book "Mastering Regular Expressions" > would be a good read it explains this concept quite completely. > > When you use Regexp#match, if you find a match, you are returned a > MatchData object which provides an [] API for standard array style > access. > > Element 0 ( [0] ) provides the COMPLETE match. If you defined any > groups in the regular expression, they will then appear as additional > elements in the MatchData [] array. Each group is then accessable > using ordinals ( [1] .. [9] I don't know what happens after 9, I've > never needed that many ) ... I believe that would be 10 :-) irb(main):003:0> m = /(((((((((((a)))))))))))/.match("a") => # irb(main):004:0> $10 => "a" irb(main):005:0> m[10] => "a" > Remember that if you define a group to be > inside an optional region of the regular expression, that group will > return nil. I'm not sure what you mean there. irb(main):009:0> /((a)?)?/.match("a").to_a => ["a", "a", "a"] David -- David A. Black dblack@wobblini.net