From: Anton Date: 2007-07-15T19:16:00+09:00 Subject: Re: [QUIZ] Maximum Sub-Array (#131) On 14/07/07, James Edward Gray II wrote: > On Jul 14, 2007, at 12:21 PM, Anton wrote: > > > Hello, everybody! I am new here. I am also relatively new to ruby. > > > > I don't know if it's OK to ask questions here before the 48 hours > > period ends. So I am going to shoot straight and ask. > > > > I am trying to create a perfect regexp match string to match an array > > string like [1, -1, 4, 5] > > So far the best I've came up with is /^\[-*\d+,*.*\]$/ > > > > If I add \s+ the string doesn't match. /^\[-*\d+,*\s+.*\]$/ And I have > > no idea why... > > Well, if I understand right, you want a regex to match an array of > one or more integers? I would probably use: > > /\A\s*\[\s*-?\d+(?:\s*,\s*-?\d+)*\s*\]\s*\Z/ > > Is that what you are after? > > James Edward Gray II > Yes, you understood me right. Your example is what I needed. But I have one small question. Why is '?' needed at the beginning? (?:\s*,\s*-?\d+) It seems to me it's the key for a correct match :)