[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String#scan puzzlement

Martin DeMello

7/12/2006 12:44:00 PM

I'm trying to match sequences of capitalised words - can someone
explain why the second regexp works but not the first?

irb(main):001:0> a = "And then we Went to The Next Place to be"
=> "And then we Went to The Next Place to be"
irb(main):002:0> a.scan /([A-Z]\w+\s+)+/
=> [["And "], ["Went "], ["Place "]]
irb(main):003:0> a.scan /(?:[A-Z]\w+\s+)+/
=> ["And ", "Went ", "The Next Place "]

martin

2 Answers

Carlos

7/12/2006 12:54:00 PM

0

Martin DeMello wrote:
> I'm trying to match sequences of capitalised words - can someone
> explain why the second regexp works but not the first?
>
> irb(main):001:0> a = "And then we Went to The Next Place to be"
> => "And then we Went to The Next Place to be"
> irb(main):002:0> a.scan /([A-Z]\w+\s+)+/
> => [["And "], ["Went "], ["Place "]]
> irb(main):003:0> a.scan /(?:[A-Z]\w+\s+)+/
> => ["And ", "Went ", "The Next Place "]

Because when you group, scan returns arrays with [$1, $2, $3...], etc.,
but in "to The Next Place to be" =~ /([A-Z]\w+\s+)+/ there is only $1.
Put the last "+" inside a group.
--


Martin DeMello

7/12/2006 1:00:00 PM

0

On 7/12/06, Carlos <angus@quovadis.com.ar> wrote:
> Martin DeMello wrote:
> > I'm trying to match sequences of capitalised words - can someone
> > explain why the second regexp works but not the first?
> >
> > irb(main):001:0> a = "And then we Went to The Next Place to be"
> > => "And then we Went to The Next Place to be"
> > irb(main):002:0> a.scan /([A-Z]\w+\s+)+/
> > => [["And "], ["Went "], ["Place "]]
> > irb(main):003:0> a.scan /(?:[A-Z]\w+\s+)+/
> > => ["And ", "Went ", "The Next Place "]
>
> Because when you group, scan returns arrays with [$1, $2, $3...], etc.,
> but in "to The Next Place to be" =~ /([A-Z]\w+\s+)+/ there is only $1.
> Put the last "+" inside a group.

ah! that makes things a lot clearer, thanks.

martin