"Simon Strandgaard" <neoneye / adslhome.dk> schrieb im Newsbeitrag
news:200408181437.58446.neoneye / adslhome.dk...
> On Wednesday 18 August 2004 12:31, Robert Klemme wrote:
> > "Austin Ziegler" <halostatue / gmail.com> schrieb im Newsbeitrag
> > news:9e7db91104081713254f2eb39e / mail.gmail.com...
> >
> > > This should do what you want.
> > >
> > > -austin
> > >
> > > str = '<span id="1"> <span>...</span> </span>'
> > > re = /(<(\/?)span>)/i
> > >
> > > str.scan(re) # => [["<span>", ""], ["</span>", "/"], ["</span>", "/"]]
> > >
> > > matches = []
> > > str.scan(re) do
> > >   matches << Regexp.last_match
> > > end
> > >
> > > matches.each do |match|
> > >   match.captures.each_with_index do |capture, ii|
> > >     soff, eoff = match.offset(ii + 1)
> > >     puts %Q("#{capture}" #{soff} .. #{eoff})
> > >   end
> > > end
> >
> > While that works, isn't it ridiculous that one has to resort to a class
> > method ("Regexp.last_match")?  I mean, there should rather be something
> > like
> >
> > /o/.each( "foo" ) do |md|
> >   # md is MatchData
> > end
> >
> > Or even
> >
> > /o/.matcher( "foo" ).each do |md|
> >   # md is MatchData
> > end
> >
> > That way Matcher could implement Enumerable.
> >
> > Sounds like a candidate for a RCR.  Any comments?
>
>
> What about $~ ?
>
> bash-2.05b$ ruby a.rb
> [[0, 13], [14, 20], [23, 30], [31, 38]]
> bash-2.05b$ expand -t2 a.rb
> str = '<span id="1"> <span>...</span> </span>'
> re = Regexp.new('(<(\/?)span[^\n/]*?>)', true) # match start or end tag
> positions = []
> str.scan(re) do
>   positions << [$~.begin(0), $~.end(0)]
> end
> p positionsbash-2.05b$

This has the same problem, only that in this case you don't use a class
method but a global variable.  Both of them are not in any way connected to
the regexp you use other than through a hidden side effect of the matching
process.  I like more explicit connection similar to the one I suggested.

Kind regards

    robert