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$


--
Simon Strandgaard