Hello Ryan

In message "Do You Understand Regular Expressions?"
   on 21.06.2007, Ryan Mcdonald <ryemcdonald / gmail.com> writes:

RM> I agree. Can someone explain why gsub, sub or scan matches with * are
RM> different than =~ matches with *

RM> puts "hello".gsub(/[aeiou]/, '<\1>')  # h<>ll<>

irb(main):024:0> "hello".gsub( /([aeiou])/, "<\\1>" )

Please note the () around the expression.
After that you can refer with \\1 to the found
letters.


RM> puts "hello".gsub(/.*/, '<\1>')       # <><>

irb(main):029:0> "hello".gsub(/(.*)/, '<\1>')
=> "<hello><>"
irb(main):030:0> "hello".gsub(/(.+)/, '<\1>')
=> "<hello>"

RM> print "before: #{$`}\n"               # before: hello

irb(main):031:0> $`
=> ""

RM> print "match:  #{$&}\n"               # match:

irb(main):032:0> $&
=> "hello"

RM> print "after:  #{$'}\n"               # after:

irb(main):033:0> $'
=> ""


hope this helps.

regards.
Karl-Heinz