# Re: Maximum Sub-Array (#131)

**URL:** <https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426>\
**Category:** Ruby\
**Created:** [July 14, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426 "2007-07-14T19:25:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 14, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/1 "2007-07-14T19:25:18Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 14, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/2 "2007-07-14T20:00:20Z")

</div>

On Jul 14, 2007, at 12:21 PM, Anton wrote:

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

---

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 15, 2007, 12:16pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/3 "2007-07-15T12:16:30Z")

</div>

On 14/07/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> James Edward G. 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 🙂

---

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 15, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/4 "2007-07-15T13:51:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 15, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/5 "2007-07-15T17:16:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@anton](https://www.ruby-forum.com/u/anton)\
**Post date:** [July 15, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/re-maximum-sub-array-131/109426/6 "2007-07-15T19:08:01Z")

</div>

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

> > > string like [1, -1, 4, 5]
> > 
> > Is that what you are after?
> > 
> > James Edward G. 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 G. II
