On Thu, Apr 7, 2011 at 1:58 AM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> You can also get the relative offset like this:
>
> str =3D "_foo_ _bar_"
>
> str.scan(/_(\w+)_/) do |curr_match|
> =A0md =3D Regexp.last_match
> =A0whole_match =3D md[0]
> =A0captures =3D md.captures
> =A0captures.each do |capture|
> =A0 =A0p [whole_match, capture, whole_match.index(capture)]
> end

That's nice!  I wasn't aware of this.  Thanks for sharing!

I also just read this in the docs:

"Note that the last_match is local to the thread and method scope of the me=
thod
that did the pattern match."

So forget my point about $~ being safer.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/