Discussion: Regexp Capture Access
Afficher un message
Vieux 02/04/2008, 16h31   #4
mutahhir hayat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

[Note: parts of this message were removed to make it a legal post.]

The reason why it's acting like this is because Regexes try to find the
largest possible match from your expression. Hence, your expression

%r{(\w,)+}.match('a,b,c')[0]

will return the first match (and largest) "a,b,"


Im sure you're asking this for a bigger reason, so you have to refine your
regular expression for that specific purpose. if you just want to split
based on a "," then simply do 'a,b,c'.split(',') which returns an array
["a", "b", "c"]

If any other, please post the actual problem.

HTH
Mutahhir.


On Wed, Apr 2, 2008 at 10:04 PM, Mateusz Tybura <wujciol@gmail.com> wrote:

> Try:
>
> %r{(\w,)+}.match('a,b,c').to_s[0..1] #=> "a,"
>
> 2008/4/2, Oliver Saunders <oliver.saunders@gmail.com>:
> >
> > Given that:
> >
> > %r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"
> >
> > and
> >
> > %r{(\w,)+}.match('a,b,c')[1] #=> "b,"
> >
> > How do I access the capture that contains "a,"?
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> >

>
>
> --
> My own blog (in polish) :
> wujciol.yoyo.pl
>


  Réponse avec citation
 
Page generated in 0,05908 seconds with 9 queries