From: James Edward Gray II Date: 2007-07-16T02:05:55+09:00 Subject: Re: [QUIZ] Maximum Sub-Array (#131) On Jul 15, 2007, at 5:16 AM, Anton wrote: > 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 :) (?: � ) is the same as ( � ), save that it doesn't set a capture variable. It's used for group elements of an expression together. James Edward Gray II