[QUIZ] Maximum Sub-Array (#131)

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

···

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...

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 :slight_smile:

···

On 14/07/07, James Edward Gray II <james@grayproductions.net> 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

Just to make sure you know, this isn't required of your solution.

And even if you were required to parse the arrays, you could use far
cooler tricks like eval() or str[1..str.length-1].split(",").map{|i|
i.to_a}

Aur

···

On 7/15/07, Anton <selecter@gmail.com> wrote:

On 14/07/07, James Edward Gray II <james@grayproductions.net> 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 :slight_smile:

(?: … ) 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

···

On Jul 15, 2007, at 5:16 AM, Anton wrote:

On 14/07/07, James Edward Gray II <james@grayproductions.net> 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 :slight_smile:

Thanks! I know it's not required. But still. I ask a question and
can't stand not solving a problem before I encounter it later.

···

On 15/07/07, SonOfLilit <sonoflilit@gmail.com> wrote:

Just to make sure you know, this isn't required of your solution.

And even if you were required to parse the arrays, you could use far
cooler tricks like eval() or str[1..str.length-1].split(",").map{|i|
i.to_a}

Aur

On 7/15/07, Anton <selecter@gmail.com> wrote:
> On 14/07/07, James Edward Gray II <james@grayproductions.net> 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 :slight_smile:
>