Accessing MatchData results in StringScanner

P

Phrogz

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)?
 
L

Logan Capaldo

Is it possible using #scan or any other method of StringScanner to get
access to captured sub-expressions in the supplied regexp?
=20
For example, something like:
ss =3D StringScanner.new( "Hello __World__" )
=20
while ...
if md =3D 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
=20
=20
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)?
=20
=20
=20

ss[1]
 
C

Charles Mills

Phrogz said:
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/StringScanner.html
http://www.ruby-doc.org/stdlib/libdoc/strscan/rdoc/classes/StringScanner.html#M000039

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

-Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top