[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Accessing MatchData results in StringScanner

Gavin Kistner

6/20/2005 4:32:00 PM

Is it possible using #scan or any other method of StringScanner to get
access to captured sub-expressions in the supplied regexp?

For example, something like:
ss = StringScanner.new( "Hello __World__" )

while ...
if md = ss.scan( /__([^_]+)__/ )
# I would like md to be a MatchData, not a String
# because I want access to the first capture;
# $1 (which I dislike on principle anyway) is not
# updated by the call to #scan
end
end


If the answer is 'no'...might this be a good addition? Perhaps as a new
method (in case returning a MatchData was much slower than a String)?

3 Answers

Logan Capaldo

6/20/2005 4:39:00 PM

0

On 6/20/05, Phrogz <gavin@refinery.com> wrote:
> Is it possible using #scan or any other method of StringScanner to get
> access to captured sub-expressions in the supplied regexp?
>
> For example, something like:
> ss = StringScanner.new( "Hello __World__" )
>
> while ...
> if md = ss.scan( /__([^_]+)__/ )
> # I would like md to be a MatchData, not a String
> # because I want access to the first capture;
> # $1 (which I dislike on principle anyway) is not
> # updated by the call to #scan
> end
> end
>
>
> If the answer is 'no'...might this be a good addition? Perhaps as a new
> method (in case returning a MatchData was much slower than a String)?
>
>
>

ss[1]


Charles Mills

6/20/2005 4:41:00 PM

0

Phrogz wrote:
> Is it possible using #scan or any other method of StringScanner to get
> access to captured sub-expressions in the supplied regexp?
>
> For example, something like:
> ss = StringScanner.new( "Hello __World__" )
>
> while ...
> if md = ss.scan( /__([^_]+)__/ )
> # I would like md to be a MatchData, not a String
> # because I want access to the first capture;
> # $1 (which I dislike on principle anyway) is not
> # updated by the call to #scan
> end
> end
>
>
> If the answer is 'no'...might this be a good addition? Perhaps as a new
> method (in case returning a MatchData was much slower than a String)?

Read the docs :)
http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/classes/StringSc...
http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/classes/StringSc...#M000039

use #[]
ss[1] is like $1

-Charlie

Gavin Kistner

6/20/2005 5:01:00 PM

0

Thanks muchly (to Logan as well)!

--
Gavin "What's RTFM mean?" Kistner