[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Grabbing quoted substrings

Gavin Kistner

12/20/2006 7:33:00 PM

From: mitchell
> > OK, I give up, what's the elegant method to
> > grab the quoted substrings in the following,
> >
> > a = 'variables="x", "y", "z", "rho", "u", "v", "w",
> "p/pinf", "s", "mach"'
> >
> > I want an array like,
> >
> > ["x", "y", "z", ..., "mach"]
> >
>
> a.scan(/"([^"]+)"/).flatten

No need to use flatten if you don't use the (unnecessary) grouping:

a.scan( /"[^"]+"/ )
or
a.scan( /".+?"/ )

(Both, of course, assuming no escaped backslashes in the content. It's
not hard to come up with a regexp that accepts those, though at some
point perhaps Ara's eval solution is easier.)

1 Answer

Tom Werner

12/20/2006 7:40:00 PM

0

Gavin Kistner wrote:
> From: mitchell
>
>>> OK, I give up, what's the elegant method to
>>> grab the quoted substrings in the following,
>>>
>>> a = 'variables="x", "y", "z", "rho", "u", "v", "w",
>>>
>> "p/pinf", "s", "mach"'
>>
>>> I want an array like,
>>>
>>> ["x", "y", "z", ..., "mach"]
>>>
>>>
>> a.scan(/"([^"]+)"/).flatten
>>
>
> No need to use flatten if you don't use the (unnecessary) grouping:
>
> a.scan( /"[^"]+"/ )
> or
> a.scan( /".+?"/ )
>
> (Both, of course, assuming no escaped backslashes in the content. It's
> not hard to come up with a regexp that accepts those, though at some
> point perhaps Ara's eval solution is easier.)
>
>
>

Though that will capture the surrounding quotes as well, which was not
specified in the requirements.

Tom Werner